[ASP.NET MVC] Routing – Prioritizing Controllers by Namespaces

The main purpose of routing in MVC application is to determine which controller and action will take care of an incoming request. When you map the routes using the “MapRoute“, you are specifying the URL pattern with a controller’s name. In a large application, there might be a chance that the same controller names exist in different namespaces. In that case, MVC will throw an exception.

1. Controllers in Different Namespaces

Controllers are just classes derived from “System.Web.Mvc.Controller“. Suppose you defined the controller “ProductController” in the “MyCom.Team1” namespace and another developer defines the another “ProductController” in the “MyCom.Team2” namespace.

The solution will be compiled successfully, but you will get an exception at runtime.

One solution is to change the name of one of the controllers. But this might turn out to be the best or even good solution. You do not want to name the controller like “Product1Controller” or “Product2Contoller”.

2. RouteCollectionExtensions.MapRoute Method

You can specify the namespaces as an string array to the “MapRoute()” methods.

public static Route MapRoute(this RouteCollection routes, string name, string url, string[] namespaces)

public static Route MapRoute(this RouteCollection routes, string name, string url, Object defaults, string[] namespaces)

public static Route MapRoute(this RouteCollection routes, string name, string url, Object defaults, Object constraints, string[] namespaces)

If you specify the namespaces, the controllers in those namespaces have higher priorities. Note that the specified namespaces have the same priority. Therefore, if you specify all namespaces here, MVC still cannot find the right controller.

3. Prioritizing Controllers by Namespaces

public static void RegisterRoutes(RouteCollection routes)
{
  routes.MapRoute("SpecialRoute", "Special/{action}/{id}/{*catchall}",
    new { controller = "Product", action = "Index", id = UrlParameter.Optional },
    new[] { "MyCom.Team1"});

  routes.MapRoute("GeneralRoute", "Product/{action}/{id}",
    new { controller = "Product", action = "Index", id = UrlParameter.Optional },
    new[] { "MyCom.Team2" }); }

By specifying the namespaces, the same “Product” controller pattern can be mapped correctly.

4. UseNamespaceFallback

The MapRoute() method returns the “Route” object that is created.

By setting the option, you can let MVC routing engine to look only in the namespaces that we specify.

  • public RouteValueDictionary DataTokens { get; set; }

The “Route” class has the “DataTokens” property. By setting the “UseNamespaceFallback” option to false, you can disable searching for controllers in other namespaces.

public static void RegisterRoutes(RouteCollection routes)
{
  routes.MapRoute(...)
    .DataTokens["UseNamespaceFallback"] = false;
}

One thought on “[ASP.NET MVC] Routing – Prioritizing Controllers by Namespaces

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s