Overview
In a traditional ASP.net application, a request is mapped to an. aspx file and corresponds to a specific physical ASPX file. In the ASP.net MVC framework, when a request is entered, it is no longer mapped to an. aspx file, but instead is mapped to the correct controller and action execution requests by the path selection engine provided in the MVC framework. The URL path mapping rule in the ASP.net MVC framework is defined in Global.asax, which brings a lot of flexibility to the program, and if you want to modify the URL application structure, you just need to modify the mapping rules. There is no need to modify the code in controller and view, but this is still not flexible enough. Changing the Global.asax code will still cause the application to recompile, and the end of this article will explain how to define the mapping rules by HttpModule in web.config.
Understanding Path Selection
Let's take a look at the following picture:
As you can see from the previous illustration, the first step after a request is a path selection, and the mapping rule is registered by adding a route instance to the routes collection, such as:
RouteTable.Routes.Add (
new Route
{
Url = "[controller]/[action]",
Defaults = new { action = "Index"},
RouteHandler = typeof (MvcRouteHandler)
});
There are four attributes in the route, which are defined as follows:
public class Route
{
public Route();
public Route(string url, Type routeHandler);
public Route(string url, object defaults, Type routeHandler);
public object Defaults { get; set; }
public Type RouteHandler { get; set; }
public string Url { get; set; }
public object Validation { get; set; }
}