Tutorial :RouteTable.Routes RouteCollection?



Question:

The example I see everywhere for MVC-style routing is something like this:

void Application_Start(object sender, EventArgs e)   {      RegisterRoutes(RouteTable.Routes);  }    public static void RegisterRoutes(RouteCollection routes)  {      routes.Add(new Route      (           "Category/{action}/{categoryName}"           , new CategoryRouteHandler()      ));  }  

What's the reason to pass the RouteTable.Routes collection to RegisterRoutes()? Why not just:

void Application_Start(object sender, EventArgs e)   {      RegisterRoutes();  }    public static void RegisterRoutes()  {      RouteTable.Routes.Add(new Route      (           "Category/{action}/{categoryName}"           , new CategoryRouteHandler()      ));  }  

What RouteCollection besides RouteTable.Routes would a route be added to? Isn't RouteTable.Routes the RouteCollection for the web application?

I have a particular IRouteHandler with has a Map() method:

public class ChatRouteHandler : IRouteHandler  {      private static bool mapped;        public void Map()      {          if (!ChatRouteHandler.mapped)          {              RouteTable.Routes.Add              (                  new Route("chat/{room}/{date}",                   new ChatRouteHandler())              );          }      }  

Is there a reason that Map() should accept a RouteCollection and not add to the RouteTable.Routes collection? Again, what other RouteCollection would this IRouteHandler be added to?


Solution:1

Unit testing.

By decoupling (through argument passing) registration from RouteTable.Routes you can choose which RouteCollection is used for registration in unit testing.

IMHO, it's also nicer to write routes.Add() instead of RouteTable.Routes.Add() everywhere.

"Is there a reason that Map() should accept a RouteCollection and not add to the RouteTable.Routes collection?"

I'm interpreting this question as "Why doesn't RouteCollection have the MapRoute method, instead of an extension method?". Yeah, RouteCollection is in System.WEb.Routing, whilst MapRoute extension method is in System.Web.Mvc. MapRoute() depends on MvcHandler, which System.Web.Routing has no idea about.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »