views:

50

answers:

2

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 Routes 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+ Routes 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.)

A: 

I would have stored the route information externally in xml file or database and loaded it in global.asax. I can add extra column/attribute that will have example url being routed so that I can search it quickly. Not to mention, I can update route information w/o rebuilding the project (of course, if there are new controllers, views etc then hey would need to be packaged in a new dll).

VinayC
+1  A: 

I'm sorry, but what are you talking about? :P

routes.MapRoute(
    "Default", 
    "{controller}/{action}/{id}", 
    new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
);

This single route entry effectively allows you to call Any action method of Any controller with any parameters.


Is this a better solution?

Probably not.

Is there any good solution?

Yes. But you need to share more of your routes to be sure.

Should we just keep adding routes to Global.asax.cs; should we give the prototype the green light; or should we do something else?

No, No, and Yes. You need to share a decent amount of routes to have a better idea of the situation.

(Assume that you cannot refactor existing route URLs to make them more logical.)

But you can make sure new ones fit in the default route, so the problem doesn't keep growing i.e. {controller}/{action}

eglasius