In order to investigate whether ASP. net mvc is suitable for application in company projects, we have studied it for a while. I feel that there are few practices in online materials. I will summarize them here for future reference.
ASP. net mvc contains a powerful URL routing engine that allows you to customize the Controller class to be used and control which action method to call based on different parameters. ASP. net mvc has a set of default rules to simplify the call of control classes and action methods. If you do not understand this default rule, it is easy to confuse in use. We use ASP. net mvc template to briefly understand these default rules. When we create an ASP. net mvc application based on the ASP. net mvc template, we can find the following code in the Global. asax file:
Public static void RegisterRoutes (RouteCollection routes) { Routes. IgnoreRoute ("{resource}. axd/{* pathInfo }");
Routes. MapRoute ( "Default", // Route name "{Controller}/{action}/{id}", // URL with parameters New {controller = "Home", action = "Index", id = ""} // Parameter defaults );
}
Protected void Application_Start () { RegisterRoutes (RouteTable. Routes ); } |
We know that Application_Start is called before the first request to access the website, so the website in this example is started and has registered a route website system.
Routes. mapRoute ("Default", "{controller}/{action}/{id}", new {controller = "Home", action = "Index ", id = ""}); the first parameter of the method is the name of the route, which is temporarily unavailable. If the name cannot be repeated when multiple routes are mapped, the second parameter is the Url format, the third parameter is an anonymous object, which expresses the default controller, action, and id corresponding to the second parameter format, here we will use a table to further explain:
URL Controler class Action method input parameters /Home/Index/5 HomeController Index (int id) 5 /Home/Edit/5 HomeController Edit (int id) 5 /Home/Index HomeController Index () None /Home HomeController Index () None /HomeController Index () None |
Observe this table. If we enter the http // localhost: 4804/Home/Index/5 address, the route will call the Index (5) method of HomeController, when the input address is http // localhost: 4804, the route will be based on the anonymous object new {controller = "Home", action = "Index ", the default value in id = ""} to select the corresponding Controller and action.
When we create the first ASP. net mvc application, we can find a default. aspx. This file is used to simplify IIS deployment during website deployment. View default. aspx. we can see the following code in the cs file:
Public void Page_Load (object sender, System. EventArgs e) { // Change the current path so that the Routing handler can correctly interpret // The request, then restore the original path so that the OutputCache module // Can correctly process the response (if caching is enabled ).
String originalPath = Request. Path; HttpContext. Current. RewritePath (Request. ApplicationPath, false ); IHttpHandler httpHandler = new MvcHttpHandler (); HttpHandler. ProcessRequest (HttpContext. Current ); HttpContext. Current. RewritePath (originalPath, false ); } |
Read this code carefully and you will find that its function is to convert the address to the root, that is, to convert http // localhost: 4804/default. aspx is converted to http // localhost: 4804/, then the route can select the corresponding Controller and action according to the url.