Jul 21, 2015

How route table creates in MVC?

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. The RouteTable uses a RouteCollection in order to store all the URL routing information it needs to accurately direct URI's to the correct controller action.

In your global.asax you will register the routes that will map to various controller actions by specifying the following:

///



/// Executed when the application starts.
///
protected void Application_Start()
{
RegisterRoutes(RouteTable.Routes);
}
Then a route will be added to the RouteCollection in the following way:

///

/// Registers the routes used by the application.
///
/// Routes to register.
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapRoute(
"Error",
"Error",
new { controller = "Error", action = "Error" });
}
This shows how the actual route information is stored in a RouteCollection, which in turn is referenced via the RouteTable.


When a MVC application first starts, the application _start() method is called. This method in turn calls RegisterRoutes() method which creates Route table.

No comments:

Post a Comment