Is there any best practice for how to best define and organize routes in MVC?
The company I work for runs a very extensive, complex ecommerce site with ~600K unique visitors/day.
Here's the problem: in our Global.asax.cs, we've got this HUGE list of approximately 75 route definitions in our RegisterRoutes()
:
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Is there a better way to define these routes other than having this gigantic list in the Global.asax.cs?
Because we've got a bunch of developers and half of them are retarded and I can't go back refactoring these routes, it can take literally a couple minutes to figure out what controller is responsible for delivering a URL's View.
What can I do?
One developer toiled away building a prototype that allows us to do this in our Global.asax.cs:
public static void RegisterRoutes(RouteCollection routes)
{
routes.Include(new RootController());
routes.Include(new AccountController());
routes.Include(new HelpController());
routes.Include(new SearchController());
// etc., for each controller
}
In this prototype, Include()
is an extension method and all Controllers inherit from IRoutedController
, which provides Include()
an IEnumerable<Route>
list of Route
s to add to the RouteCollection
.
But with this prototype, we have a new problem: instead of looking through a list of route.MapRoute()
calls to find which controller a specific URL invokes, we now have to guess which Controller is responsible for a specific URL and check its IRoutedController
list of routes to see if the URL actually invokes that Controller we guessed. Not so hard, but sometimes takes just as long as examining our list of 75+ Route
s in Global.asax.cs.
Is this a better solution?
Is there any good solution?
Should we just keep adding routes to Global.asax.cs; should we give the prototype the green light; or should we do something else? (Assume that you cannot refactor existing route URLs to make them more logical.)