Routing in Asp.NET MVC is how ASP.NET MVC matches a URI to an action. ASP.NET routing enables you to use URLs that do not have to map to specific files in a Web site. Because the URL does not have to map to a file, you can use URLs that are descriptive of the user's action and therefore are more easily understood by users. It is a part of all the MVC versions but MVC 5 supports a new type of routing, called attribute routing. As the name implies, attribute routing uses attributes to define routes. Attribute routing gives you more control over the URIs in your web application. There is a separate article to understand Attributes in Routing in ASP.NET MVC.
How to define Routing in MVC?
If you adopt the MVC convention of implementing controllers by creating classes that derive from the ControllerBase class and giving them names that end with "Controller", you do not need to manually add routes in an MVC application. The preconfigured routes will invoke the action methods that you implement in the controller classes.
You may also want to add custom routes in an MVC application, than you need to use the MapRoute(RouteCollection, String, String) method instead of the MapPageRoute(String, String, String) method. Once you have decide the Routing for your application, you need to register it and make sure that you are calling this register method at Application_Start() event of your application. The following example shows the code that creates default MVC routes in the Global.asax file, as defined in the Visual Studio project template for MVC applications.
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 value for above URL
);
}
protected void Application_Start()
{
RegisterRoutes(RouteTable.Routes);
}
}
Always remember route name should be unique across the entire application. Route name can’t be duplicate.
How to set Default Value for Routing in ASP.NET MVC?
Now you are able to define the routing in MVC. When you define a route, than you can still assign a default value for a parameter. This default value is used if a value for that parameter is not included in the URL. You can set default values for a route by assigning a dictionary object to the Defaults property of the Route class. The following example shows how to add a route that has default values, by using the MapPageRoute(String, String, String, Boolean, RouteValueDictionary) method.
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapPageRoute("",
"Category/{action}/{categoryName}",
"~/categories.aspx",
true,
new RouteValueDictionary
{{"categoryName", "MVC"}, {"action", "show"}});
}
void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
}
As you can see in above code, ASP.NET routing handles a URL request, the route definition shown in the example (with default values of MVC for categoryName and show for action) produces the results that are listed in the following table.
URL | Parameter values |
---|
/Category | action = "show" (default value) categoryName = "MVC" (default value) |
/Category/add | action = "add" categoryName = "MVC" (default value) |
/Category/add/routingexample | action = "add" categoryName= "routingexample" |
How Routing is work in ASP.NET MVC?
To make better understanding of work flow of MVC Routing consider a diagram given below.
In above diagram, you can see that when any user make request for any URL, the system check in RouteTable.Routes (Collection of all the Routes). It parses the route and match with current URL. If there are any constraints or attributes are found than it process the constraints and check whether it satisfy than it will Build and returns RouteData and page will be served , otherwise it will returns null and Page Not Found with Status code 404 can be returned.
To understand how Routing is works in MVC at macro level, we have following diagram shows how it is done.
The main function of the UrlRoutingModule is to tell the IIS to use IHttpHandler which I got from the matched route. And IIS do the same and returns the result to the browser.
Summary: I Hope you have enjoyed this article and now you are able to define routing in ASP.NET MVC application. Although, you need to know something more about attributes in Routing. Thus I have written this in a separate article to understand attributes in Routing in ASP.NET MVC.