Branislav Abadjimarinov's technical blog

Branislav Abadjimarinov's technical blog - Browsing ASP.NET MVC

  1. Web Applications Development with .NET Framework and ASP.NET course in Technical University of Sofia

    From Monday, 15 February a new course on programming is starting in the Technical University of Sofia. The course is titled "Web Applications Development with .NET Framework and ASP.NET" and it will cover some of the basics of Microsoft .net platform and ASP.NET. The training provided will be very brief and it will require a lot of dedication by the people attending.  I'm taking part with a couple of lectures on ASP.NET and AJAX. The course is free and is supported by Telerik Academy. You can check the course official web site for more information.

    Posted by Branislav Abadjimarinov on February 11 at 1:43 AM

  2. How to render asp.net control to string

    Every control in ASP.NET inherits from System.Web.UI.Control and have the RenderControl method. When you want to get the html output of the control at any point of the page execution lifecycle you can use the following method.

    public string RenderControl(Control ctrl)
    {
        StringBuilder sb = new StringBuilder();
        StringWriter tw = new StringWriter(sb);
        HtmlTextWriter hw = new HtmlTextWriter(tw);

        ctrl.RenderControl(hw);
        return sb.ToString();
    }

    At this point ASP.NET MVC does not have full support for asp.net controls. Using this trick you can render control's html directly on the view using a html helper. While this is good for read-only controls it will not work with controls that require event handling because in ASP.NET MVC by default you are missing  the page lifecycle.

    Posted by Branislav Abadjimarinov on October 28 at 9:10 AM

  3. ASP.NET MVC default route problem

    If you are working with ASP.NET MVC you definitely have seen that in the startup project in Visual Studio in Global.asax file there is a default route added like this:

    routes.MapRoute(
    "Content",     // Route name
    "{controller}/{action}/{id}",    // URL with parameters
    new { controller = "Home", action = "Index", id = "" } // Parameter defaults
    );

    It is very useful to quickly show how the MVC works but in commercial projects I'll suggest  not to use a default route for handling /{Controller}/{Action} requests in ASP.NET MVC. By doing that you can get a ton of these exceptions:

    System.Web.HttpException: The controller for path '/MyCustomPath/' could not be found or it does not implement IController.

    The reason for this is that when you make a request for non-existing path like /MyCustomPath/ the MVC Controller factory will try to instantiate a class inheriting IController and named "MyCustomPath" and when It doesn't find it an exception will be thrown.
    If the non-existing url is in some static file like .css or .js or the html of the page you can get a lot of these exceptions in just a few seconds if the web server is under a heavy load.

    The solution is simple - just remove the default route and add explicitly routes with the controllers you actually use. By doing that you'll get a 404 without the exception.

    Posted by Branislav Abadjimarinov on October 26 at 1:09 PM

© Copyright 2017 Powered by AtomSite 1.3.0.0