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.
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
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 }
);
}
}