What is Routing in MVC
Any URL in an ASP.NET Web Forms application must correspond to a
specific.aspx file. A URL like http://domain/classinfo.aspx must fit the
file classinfo.aspx, which includes code and markup for making a browser
response.
Routing is a concept that isn't exclusive to the MVC system. It can be used
for either an ASP.NET Webform or an MVC application.
Routing was implemented in ASP.NET to remove the need to map each URL to a
physical file. We may describe a URL pattern that corresponds to the request
handler using routing. A file or a class may be used as the request handler.
The request handler in an ASP.NET Webform application is the.aspx file, while
in MVC, it is the Controller class and Action method.
For example In ASP.NET Webforms http://domain/students can be mapped to
http://domain/studentsinfo.aspx, and in MVC, the same URL can be mapped to the
Student Controller and Index action process.
Route
The URL pattern and handler information are defined by the route. The
Routing engine can use the RouteTable to decide the appropriate handler
class or file for an incoming request based on all of an application's
configured routes.
The Routing method is depicted in the diagram below.
Configure a Route
Any MVC application must set up (register) at least one of the
MVC framework's default routes. You can register a route in the
RouteConfig class, which is located in the RouteConfig.cs
under App_Start folder. The following diagram shows how to use the RouteConfig class to
configure a route.
The route is configured using the MapRoute() extension method of
RouteCollection, where the name is "Default," the url pattern is
"controller/action/id," and the defaults parameter for controller,
action method, and id parameter are all set to defaults. If a
controller, action process, or value of the id parameter does not
exist in the incoming request URL, the defaults specify which one
should be used.
The MapRoute() method of the RouteCollection class can be used to
configure other routes in the same way. This RouteCollection is
actually a RouteTable class property.
URL Pattern
Only after the domain name portion of the URL is considered is the URL
pattern. For example, localhost:1010/controller/action/id will be the
URL pattern for "controller/action/id." Something that comes after
"localhost:1010/" is a controller name. Similarly, anything after the
controller name is called the action name, followed by the value of
the id parameter.
If the URL does not contain anything after the domain name, the
request will be handled by the default controller and action method.
For example, as configured in the default parameter,
http://localhost:1234 will be handled by the HomeController and the
Index() process.
Considering the above default path, the table below shows which
Controller, Action method, and Id parameter will handle which URLs.
| URL | Controller | Action | Id |
|---|---|---|---|
| http://localhost:1010/home | HomeController | Index | null |
| http://localhost:1010/home/index/123 | HomeController | Index | 123 |
| http://localhost:1010/home/about | HomeController | About | null |
| http://localhost:1010/home/contact | HomeController | Contact | null |
| http://localhost:1010/teacher | TeacherController | Index | null |
| http://localhost:1010/teacher/edit/123 | TeacherController | Edit | 123 |
Multiple Routes
The MapRoute extension method can also be used to build a custom route. In MapRoute, you must have at least two parameters: route name and URL pattern. Defaults is an optional parameter.
You can create several custom routes, each with its own name. Consider the following scenario, in which the "Teacher" route is recorded.
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Teacher",
url: "teachers/{id}",
defaults: new { controller = "Teacher", action = "Index"}
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
} The URL pattern for the Teacher route is teacher/id, which specifies that any URL that begins with domainName/teachers must be handled by the TeacherController, as shown in the above code.
Since we want any URL that starts with teacher to always use the TeacherController class's Index() action, we haven't mentioned action in the URL pattern. Any URL request that begins with domainname/teachers will be handled by the default controller and action.
Each route is evaluated sequentially by the MVC system. It begins with the first configured route, and if the incoming URL does not match the route's URL pattern, it moves on to the second route, and so on.
Route Constraints
By configuring route constraints, you can also add restrictions on the value of the parameter. for example the following route places a restriction on the id parameter requiring that its value must be numeric.
routes.MapRoute(
name: "Teacher",
url: "teacher/{id}/{name}/{standardId}",
defaults: new { controller = "Teacher", action = "Index", id = UrlParameter.Optional, name = UrlParameter.Optional, standardId = UrlParameter.Optional },
constraints: new { id = @"\d+" }
);If you specify a non-numeric value for the id parameter, the request will be handled by another route or, if no matching routes exist, then "The resource could not be identified" error will be displayed.
Register Routes
After configuring all of the routes in the RouteConfig class, you must register it in the Global.asax Application Start() event so that all of your routes are included in the RouteTable.
public class Mvc : System.Web.HttpApplication
{
protected void Application_Start()
{
RouteConfig.RegisterRoutes(RouteTable.Routes);
}
}

0 Comments