Tuesday, June 21, 2011

Overview of ASP.NET MVC Routing

In this tutorial, we introduce an important feature of all ASP.NET applications ASP.NET MVC Routing call. The ASP.NET routing module is responsible for assigning incoming requests particular browser MVC controller actions. At the end of this tutorial, you will understand how the standard routing table maps requests to controller actions.

Using the default route table

When you create a new ASP.NET MVC, the application is configured to use ASP.NET Routing. ASP.NET routing is configured in two places.

First, ASP.NET routing is enabled on the file from the Web application configuration (Web.config file). There are four sections in the configuration file that are relevant for routing: the system.web section. httpModules, section system.web.httpHandlers, system.webserver.modules section and section system.webserver.handlers. Be careful not to delete these sections, because without these sections routing no longer work.

Second, and most importantly, a routing table is created in the application's Global.asax file. The Global.asax file is a special file that contains event handlers for the events of the life cycle of the ASP.NET application. The routing table is created during the application start event.

The file in Listing 1 contains the default Global.asax file for an ASP.NET MVC application.


Listing 1 - Global.asax.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace MvcApplication1
{
// Note: For instructions on enabling IIS6 or IIS7 classic mode,
// visit http://go.microsoft.com/?LinkId=9394801

public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);

}

protected void Application_Start()
{
RegisterRoutes(RouteTable.Routes);
}
}
}



When an MVC application first starts, the method Application_Start () is called. This method in turn calls the RegisterRoutes () method. The RegisterRoutes () creates the routing table.

The routing table contains a single default route (default name). The default route maps the first part of a URL for a driver name, the second segment of the URL of a controller action, and the third segment with a parameter named id.

Imagine you enter the following URL into the address bar of your browser:

/ Home/Index/3

Maps of the default route this URL for the following parameters:

controller = Home

action = index

id = 3

When you request the URL / Home/Index/3, the code runs as follows:

HomeController.Index (3)

The default route includes defaults for all three parameters. Failure to provide a driver, then the default parameters of the controller to the brokerage. Failure to provide an action, the default parameters of action for the value index. Finally, failure to provide an identifier, the id parameter defaults to an empty string.

Here are some examples of how the default route maps URLs to controller actions. Imagine you enter the following URL into the address bar of your browser:

/ Home

Because the default the default path, entering this URL will cause the index () class in Schedule 2 HomeController called.


Listing 2 - HomeController.cs

using System.Web.Mvc;

namespace MvcApplication1.Controllers
{
[HandleError]
public class HomeController : Controller
{
public ActionResult Index(string id)
{
return View();
}
}
}

In Listing 2, the class includes a method called HomeController Index () that accepts one parameter named ID. The URL / Home causes the index () to call an empty string as the parameter id.

Due to the way the MVC framework invokes the actions of the driver, the URL / Home also matches the index () HomeController class in Listing 3.


Listing 3 - HomeController.cs (Index action with no parameter)

using System.Web.Mvc;

namespace MvcApplication1.Controllers
{
[HandleError]
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
}
}


The index () method in Listing 3 does not accept any parameters. The URL / Home will make the index () to be called. The URL / Home/Index/3 also invokes this method (the ID is ignored).

The URL / Home also matches the index () HomeController class in Listing 4.


Listing 4 - HomeController.cs (Index action with nullable parameter)

using System.Web.Mvc;

namespace MvcApplication1.Controllers
{
[HandleError]
public class HomeController : Controller
{
public ActionResult Index(int? id)
{
return View();
}
}
}



In Listing 4, the method index () has an integer parameter. Because the parameter is nullable (can be set to zero), the index () can be called without causing an error.

Finally, invoking the index () method in Listing 5 to the URL / Home causes an exception as the parameter identification is not a parameter accepts null values. If you try to invoke the method index () then the error is show.


The URL / Home/Index/3 other hand, works very well with the controller action index List 5. Application / Home/Index/3 makes the method index () is called with a parameter ID that has the value 3.

Summary

The aim of this tutorial is to provide a brief introduction to ASP.NET Routing. We examined the routing table by default you get with a new ASP.NET MVC. He has learned default route maps URLs to controller actions.