.netasp.net-mvccustomizationmaproute

ASP.NET MVC customization - Inherit or alter Framework


If you wanted to alter the way that routes were processed can you do that in a MVC project OR do you have to alter the MVC Framework?

For example lets say you have the standard code in RegisterRoutes of Global.asax.cs:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

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

}

But lets say what you really want to happen is to have your custom class intercept all incoming requests and route them in a special way. Can this be done with via Inheriting from a MVC framework class and then customizing, or do you have to actually alter the framework code?


Solution

  • Extending, you just need to write custom RouteHandler.

    Look at:

    ASP.NET MVC, Manipulating URL Structure

    Developing Custom RouteHandler

    Or planety other on the web.

    EDIT:

    You can do even more by extending RouteBase and adding it to routes.

    routes.Add("CoolRouter", new CoolRouter());