In this tutorial, we introduce you to each ASP. An important feature is called URL routing. The URL routing module is responsible for mapping from browser requests to specific controller actions.
In the first part of the tutorial, you will learn how the standard routing table maps to the actions of the controller. In the second part of the tutorial, you will learn how to modify the default routing table to become a custom route.
Using the default route table
When you create a new ASP. NET MVC application, the application has configured the default URL route. URL routing is set in two places.
First, URL routing is configured in your application Web configuration file (the Web. config file). There are four configuration sections for routing in the file: System.web.httpModules section, system.web.httpHandlers section, System.webserver.modules section, and System.webserver.handlers Festival. Be careful not to remove these configuration sections because none of these configuration section routes will work.
Second, more importantly, a routing table is created in the application's Global.asax file. The Global.asax file is a special file that contains ASP. NET application life-cycle event handling. When the application start event begins, the route is created.
Figure 1 is the default Global.asax file for an ASP. NET MVC application.
Figure 1–global.asax.cs
The Application_Start () method is called when an ASP. NET MVC application runs for the first time. This method, in turn, calls the RegisterRoutes () method, and the RegisterRoutes () method creates the routing table.
The default route contains a route (named Default). The first URL of the default route maps to a controller name, the second segment URL maps to an action, and the third segment maps to a parameter called Id.
Imagine you enter the following URL in the browser address bar:
/home/index/3
The default route maps the following parameters:
Controller = Home
Action = Index
ID = 3
When you request URL/HOME/INDEX/3, the following code will be executed:
Homecontroller.index (3)
The default route includes a default of three parameters. If you provide a controller, the default controller parameter value is Home. If you do not provide an action, the value of the default action parameter is Index. Finally, if you do not provide an ID, the ID parameter is an empty string by default.
Let's look at some examples of how default routes map URLs to controller actions. Imagine you enter the following URL in the browser address bar:
/home
Because of the default route parameter, this URL causes the HomeController class's Index () method 2 to be called.
Figure 2–homecontroller.cs
In Figure 2, the HomeController class contains a method called Index (), which accepts a single parameter called ID. Url/home is called by the Index () that causes the value to be an empty string ID parameter.
Because the ASP. NET MVC framework invokes the controller action, the url/home also matches the HomeController class Index () method in Figure 3.
Figure 3–homecontroller.cs (Index action with no parameters)
The index () method in Figure 3 does not accept any parameters. Url/home will cause the method of index () to be called. URL/HOME/INDEX/3 also calls this method. (ID is ignored)
Url/home also matches the HomeController Class index () method in Figure 4
Figure 4–homecontroller.cs (nullable parameter, index action)
In Figure 4, Index () has an integer method. Because the parameter is a nullable type (the value can be null), the Index () call cannot produce an error.
Finally, in Figure 5, call Index (), Url/home causes an exception from the ID parameter is not a nullable type parameter, and if you try to call the index () method, you have to come up with an error page:
Figure 5–homecontroller.cs (Index action with)
URL/HOME/INDEX/3, on the other hand, the Controller Index action in Figure 5 can be working. The request/HOME/INDEX/3 raised with a value of 3 for the Index () method is called.
Create a custom route
The default routing table is sufficient for many of the simplified-disk ASP. NET MVC applications. However, you may find that you have special routing needs. In this case, you can create a custom route.
Imagine, for example, that you write a blog application, and you might want to deal with requests like this:
/archive/12-25-2009
When a user initiates this request, you return a blog that matches the 12/25/2009 date. So to handle such a request, you need to create a custom route.
The Global.asax file in Figure 6 contains a new custom route called a blog that handles requests like this/archive/entry date .
Figure 6–global.asax (custom route)
It is important that you increase the routing order in the routing table. Our custom blog route is increased before the default route. If you do, the default route will always replace calling a custom route.
The custom blog route matches any/archive/start request, so it matches all of the following URLs:
/archive/12-25-2009
/archive/10-6-2004
/archive/apple
This custom route maps the incoming request to a call to the archive controller while calling the entry () method, and when the entry () method is called, the entry date is converted to a named EntryDate parameter.
In Figure 7 You can use the Controller and blog to customize the route.
Figure 7–archivecontroller.cs
Note the entry () method in Figure 7 accepts a datetime-type parameter. The ASP. NET MVC Framework is intelligent and can be automatically converted from the datetime value of the URL to entry date. If the entry date from the URL cannot be converted to datetime, an error is drawn.
Summarize
The purpose of the tutorial is to provide you with a brief introduction to URL routing. First, we examined the default routing table you got for a new ASP. NET MVC application. You learned how the default route table maps URLs to controller actions.
Next, you learned how to create a custom routing table. You learned how to add a custom route table to the Global.asax file to describe the entry point of the blog. We discuss how to map the motion of the request blog entry point to a controller named Archivecontroller called entry ().
Introduction to URL routing for ASP.