views:

45

answers:

4

I was reading the following answer about regions on the topic of programming standards:

http://programmers.stackexchange.com/questions/1338/worst-coding-standard-youve-ever-had-to-follow/1937#1937

I tend to agree with what @Cumbayah is saying, but I have to admit that there's one place where I'm relying on regions to keep the code tidy. That's in the Global.asax to keep my long list of routes somewhat legible.

The thing is, there's 5 - 10 Controllers and each has a handfull of routes into it, some specifically for certain action methods and others more general. So with that we're talking ~30,40,50ish route definitions defined in a single method in Global.asax. Right now I have them divided into different #Regions for each Controller to tidy it up a bit, but there sure is a stink off it! Is there a better way for me to do this?

+1  A: 

I think you want to look into so-called "Areas"; they should allow you to segment off code into, you guessed it, areas, and specify the routing and other items within.

Noon Silk
+2  A: 

Besides areas, if you have ~5-10 for each controller, what's wrong with:

ConfigureAccountControllerRoutes(routes);
ConfigureSomeOtherControllerRoutes(routes);

Or like in my current project, its not even in the global.asax, but in a class dedicated to configure the routes. That was recently introduced when we started needing the global.asax to do something else besides routes + error logging.

eglasius
+3  A: 

1) As per Noon, consider the use of Areas

2) As per eglasius, consider moving the route registration concerns out of global.asax and into a separate file. Check out the RouteInitialiser class in the "Who Can Help Me" sample application for one way to do this.

3) Consider DRY'ing up your route configuration by adopting a better route registration API, like Steve Hodgkiss's restful-routing stuff http://github.com/stevehodgkiss/restful-routing

4) Don't sweat it. If you have lots of controllers, you will have lots of routes. Can't get around that ;)

Oh, and 5) Get rid of your regions, because regions suck ass ;)

Martin Aatmaa
+1 I forgot addressing the possibly unnecessary amount of routes. What I have seen is that when the route count is really large compared to the size of the project, there are usually convoluted ways the urls / actions / actionresults / others / are being used. One of such is overuse of constraints, those are fine when you need to make sure a route isn't activated in some very specific scenarios, in other you are usually 1000 times better by having such checks in the controller methods or if needed in some action filters.
eglasius
A: 

This is a quick hack I use:

Just make a static function in your controller to set the routes, like this:

public static void SetRoutes (ref RouteCollection routes)
{
    routes.MapRoute(...);
}

and then call this from your RegisterRoutes() function. This effectively doesn't do anything significant but it makes a large number of routes easier to maintain and lets you group routes by the controller.

ninjagod