Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RouteTable.Routes RouteCollection?

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?

like image 947
core Avatar asked Mar 30 '09 04:03

core


People also ask

What is Route collection?

The RouteTable is a class that stores the URL routes for your application. A RouteCollection provides a collection of route information to be used when mapping a URI to a controller action. The RouteTable contains a property called Routes that will return a RouteCollection .

What does a route table do?

A routing table is a set of rules, often viewed in table format, that is used to determine where data packets traveling over an Internet Protocol (IP) network will be directed. All IP-enabled devices, including routers and switches, use routing tables.

What is Route collection in MVC?

The RouteCollection class provides methods that enable you to manage a collection of objects that derive from the RouteBase class. Typically, you will use the static Routes property of the RouteTable class to retrieve a RouteCollection object. The Routes property stores all the routes for an ASP.NET application.


1 Answers

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.

like image 99
CVertex Avatar answered Oct 19 '22 23:10

CVertex