ASP. net mvc routing mechanism, asp. netmvc
Traditional ASP. NET web form is hypothetical. There is a certain association between the user request URL and the files on the server. Here, the server's job is to follow the user request received, the corresponding file is retrieved to the user. This method is suitable in the web form era, because ASP. NET pages are aspx pages and can independently reply to users' browser requests. However, this method is not suitable in MVC. In MVC, user requests are handled through the methods in the Controller. In MVC, there is no ASP. to solve this problem, we need to learn the MVC routing mechanism. The routing mechanism has two functions: 1. check the received URL request to determine the method of the Request controller. 2. generate external URLs (when a user clicks a link, a request is sent to the URL displayed on the browser through the view)
Now let's get started with MVC routing configuration:
In the MVC framework, there are two ways to create a route:
1. Agreed-based routing configuration
2. Specific route Configuration
You may already be familiar with the agreed-based routing configuration, but the specific routing configuration is newly added in MVC5. We will all learn here
The routing mechanism is unknown. What is the Controller and the Actions method? It just extracts URL fragments, and route requests are processed later, the requested page can be obtained only when the routing configuration is met;
By default, the route matches the URL with the correct segment, for example, {controller}/{action}, and matches the URL with two segments.
The URL mode is conservative and only matches URLs with the same segments. However, the URL mode is inclusive. As long as the URL has a correct segment, the value of the segment will be checked, but no matter what the value is.
The route configuration is in the RouteConfig. cs file;
public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); routes.MapRoute( name: "Student", url: "student/{id}", defaults: new { controller = "Student", action = "Index"},
constraints: new { id = @"\d+" }
); }
The static method RegisterRoutes is called in the Global. asax file. Each time the program runs, it will execute the Application_Start () method in the Global File for Route registration;
protected void Application_Start() { AreaRegistration.RegisterAllAreas(); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); }
,