The responsibility of the ASP.net routing module is to map incoming browser requests to specific MVC controller actions.
Use the default route Table
When you create a new asp.net MVC application, this application has been configured to use ASP.net Routing. ASP.net Routing is set in 2 places. The first, asp.net Routing Web configuration file (Web.config file) in your application is valid. There are 4 code snippets associated with routing in the configuration file: System.web.httpModules code snippet, system.web.httpHandlers code snippet, system.webserver.modules code snippet, and System.webserver.handlers code Snippet. Be careful not to delete these snippets, and if they are not, routing will no longer run. The second, and more important, route table is created in the application's Global.asax file. This Global.asax file is a special file that contains the event handlers for the ASP.net application lifecycle events. This route table is created in the start event of the application.
The default Global.asax file for the ASP.net MVC application is included in Listing 1.
Listing 1-global.asax.cs
Code highlighting produced by Actipro Codehighlighter (freeware)
http://www. Codehighlighter.com/--> 1 public class MvcApplication:System.Web.HttpApplication
{public
static void RegisterRoutes (routecollection routes)
{
routes. Ignoreroute ("{resource}.axd/{*pathinfo}");
Routes. Maproute (
"Default",///Routing name
"{controller}/{action}/{id}",//URL
new {controller with parameters) = "Home", action = "I Ndex ", id = urlparameter.optional}//Parameter
default value
);
}
protected void Application_Start ()
{
arearegistration.registerallareas ();
RegisterRoutes (routetable.routes);
}
When an MVC application is first started, the Application_Start () method is invoked, which in turn calls the RegisterRoutes () method.
This default route table contains a single route. This default route maps the first segment of the URL to a controller name, the second segment of the URL is mapped to a controller action, and the third segment is mapped to an argument named ID.
If you type the following URL:/HOME/INDEX/3 in the address bar of the Web browser, this default route maps the URL to the following parameter:
Controller = Home Controller name
ACTION = Index Controller action
parameter of id = 3 ID
When you request a URL like/HOME/INDEX/3, the following code executes. Homecontroller.index (3)
This default route contains 3 default parameters. If you do not provide a controller, then controller defaults to home. Similarly, the action defaults to the Index,id parameter by default to an empty string.
Let's take a look at some examples of how the default route maps URLs to controller actions. If you enter the following url:/home in your browser's address bar, because these default route parameters have some associated default values, typing such a URL will cause the index () method of the HomeController class (such as Listing 2) to be invoked.
Code
highlighting produced by Actipro Codehighlighter (freeware) http://www. Codehighlighter.com/-->
1 namespace mvcroutingapp.controllers
{
[handleerror] public
class Homecontroller:controller
{public
actionresult Index (string id)
{
viewdata[' message '] = ' welcome ASP. NET mvc! ";
return View ();
}
Public ActionResult About ()
{return
View ();
}
}
}