In this article, we will learn about routing in ASP.NET web applications. Routing plays a vital role in the ASP.NET MVC web applications.
In this article, we will discuss
By the end of this article, you have a basic understanding of Routing in ASP.NET MVC.
The ASP.NET Routing is a pattern matching mechanism that is responsible for mapping the incoming request from the browser to specific controller actions.
In the ASP.NET WebForm application, each URL must match with a particular “.aspx” file. For example, a URL “http://www.example.com/empinfo.aspx” must match with the file “empinfo.aspx” which contains the code and markup for rendering a response to the browser.
To Overcome the problem of mapping each URL to the physical file. ASP.NET introduces the Routing. In ASP.NET WebForm request handler is “.aspx” file and in ASP.NET MVC request handler is a controller and an action method.
For Example, “http://www.example.com/employees” can be mapped to “http://www.example.com/empinfo.aspx” in ASP.NET Webforms and the same URL can be mapped to Employee Controller and Index action method in MVC.
A URL contains the Following properties as follows:
The ASP.NET MVC framework comes out with a default route. The default routing template also displays the property names of the route attributes, so it is easier for a beginner to understand what’s going on. You can find the Default Routes in the App_Start folder in RouteConfig.cs file. Let’s have a look at the default route:
public static void RegisterRoutes(RouteCollection routes) {
// Ignore the Url thats end with .axd Extension
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default", // Route Name. Each Route has a Unique Name
url: "{controller}/{action}/{id}", // URL Pattern to be Mapped
// Default Values for the URL
defaults: new {
controller = "Home",
action = "Index",
id = UrlParameter.Optional
});
}
Note:
controller/action/id
”.The valid URLs for this route are as follows:
Additionally to the default route, the ASP.NET MVC template implements the routes.IgnoreRoute(“{resource}.axd/{*pathInfo}”);
.
This is used for ignoring the Url that ends with .axd
Extension or you can define other extensions to ignore routes.
There are two types of routing in ASP.NET as follows:
Convention Based Routing: To define the Convention Based Routing, we call the MapRoute
method and set its unique name, URL pattern, and specify some default values.
routes.MapRoute(
name: "Default", // Route Name, Each Route has a Unique Name
url: "{controller}/{action}/{id}", // URL Pattern to be Mapped
// Default Values for the URL
defaults: new { controller = "Home", action = "Index",
id = UrlParameter.Optional }
);
Attribute-Based Routing: The Attribute Routing introduced in ASP.NET MVC 5. To define the Attribute-Based Routing, we specify the Route attribute above the action method of the controller or above the controller. To use attribute-based routing, you need to enable it first in RouteConfig.cs file.
public static void RegisterRoutes(RouteCollection routes)
{
// Ignore the Url thats end with .axd Extension
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
}
[Route("home")]
public ActionResult Index() {
return View();
}
For most of the requests, your web application's default route will work fine, however, you might have different needs to which are not satisfied by the default routing of ASP.NET MVC. So you can create a custom route for your web applications.
For example, sometimes you need to URL structure like this as given below:
“{language}-{country}/{controller}/{action}”
“{date}/{controller}/{action}”
“blog/{articletitle}”
So you can create your own custom routing for your controller action methods.
Note: when you create a custom route for your web application, keep one thing in mind that the custom routing is always placed above the Default Route in case of Convention Based Routing.
public class RouteConfig {
public static void RegisterRoutes(RouteCollection routes) {
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
//Custom Route
routes.MapRoute(
name: "CustomRoute", url: "blog/{articletitle}", defaults: new {
Controller = "Home",
action = "articleDetails"
});
// Default Route
routes.MapRoute(
name: "Default", url: "{controller}/{action}/{id}", defaults: new {
controller = "Home",
action = "Index",
id = UrlParameter.Optional
});
}
}
Points to remember:
Application_Start()
method of Global.asax.cs file.I hope this article will help you to understand the basic concepts of routing in ASP.NET MVC.
Share your valuable feedback, please post your comment at the bottom of this article. Thank you!
Comments