1

I am working on a MVC 4 project that will serve as an API layer of a larger application.

The developers that came before me set up separate Areas to separate different API requests (i.e Search, Customers, Products, and so forth).

I am noticing that each Area has separate Area registration classes that define routes for that area. However, the routes defined are not area-specific (i.e. {controller}/{action}/{id} might be defined redundantly in a couple of areas).

My instinct would be to move all of these route definitions to a common place like the Global.asax to avoid redundancy and collisions, but I am not sure if I am correct about that.

2 Answers 2

1

Your route organization will depend entirely on what your goals are.

Some routes are organized in such a way that the resulting URLs increase search engine optimization. Others are designed for readability, or to be as short as possible. Still others are arranged hierarchically, based on the folder structure of the application.

In short, there is no "one, true way." This is why routing exists in the first place. Were there only a single right way to arrange your URLs, you wouldn't need the "patch-bay" mechanism that routing provides.


If your question is where to put the routes, you should put them where it makes most sense to you. If the areas function independently and have different routing rules, it probably makes sense for each area to have its own routes. If, on the other hand, all the areas use the same routing scheme, it probably makes sense to keep a set of global rules.

It is my understanding that ASP.NET MVC checks the area routing rules first, so if you have some rules that are specific to a certain area, you can put those rules in the area, and if none of them match, the routing engine will fall back to the global routes.

3
  • Thanks, but my question is not how to organize the URLs but the routes within the application. In other words, is it better to define all routes in the Global.asax than to define separately in the areas? Oct 3, 2012 at 17:46
  • Ah, that's a different question, isn't it? Aside from the question of where to put the routing, the routing itself is a direct consequence of your URL organization, so you can't really say that it doesn't matter. Oct 3, 2012 at 17:47
  • The last paragraph is exactly what I needed to know. Thanks! Oct 3, 2012 at 17:55
2

Short Answer: Moving area route registrations to common place is not necessary, better to keep them under specific Areas that they belong to. You may use AreaRegistration.RegisterAllAreas(); statement in Global.asax that does the registration.

I have listed and example code (for Clients area) that you may need to declare for each area in your project.

    public class ClientsAreaRegistration : AreaRegistration
        {
            public override string AreaName
            {
                get
                {
                    return "Clients";
                }
            }



     public override void RegisterArea(AreaRegistrationContext context)
        {
            context.MapRoute(
                "Clients_default",
                "Clients/{controller}/{action}/{*id}",
                new { action = "Index", id = UrlParameter.Optional }
            );
        }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.