Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

up vote 1 down vote accepted

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.

share|improve this answer
    
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? –  Matthew Patrick Cashatt Oct 3 '12 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. –  Robert Harvey Oct 3 '12 at 17:47
    
The last paragraph is exactly what I needed to know. Thanks! –  Matthew Patrick Cashatt Oct 3 '12 at 17:55

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 }
            );
        }
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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