In an ASP. NET WebForm application, the user requests a physical file, which includes both a static page and a dynamic page, and the display in the URL is a relative path to a physical file in the server. But ASP. NET MVC is different, and the user requests an action method in the controller that maps the URL to a relative controller and action through a route.
asp: When Application_Start, the rules of routing are defined, and when the user accesses using the rules of the routing rules, the user's complete URL access is realized by route mapping. Let's start with ASP. NET WebForm for a simple routing simulation.
First, we create a new ASP. NET Empty Web application
Second, add a Global.asax file and simulate the ASP. NET MVC write route code in the Application_Start event. (Note: The Application_Start event is performed once when the entire application is deployed to a server such as IIS, when the application pool is started)
protected voidApplication_Start (Objectsender, EventArgs e) { varDefaults =NewRouteValueDictionary {{"Controlls","*"}, {"Action","*"} };//Define a routing dictionaryRouteTable.Routes.MapPageRoute ("Defaults","{controller}/{action}","~/routemapping.aspx",true, defaults);//With the default route mapping, all user requests are handed over to the Routemapping.aspx WebForm page during the entire simulated route. }
Third, the user request "{controller}/{action}" format URL path, such as HTTP://LOCALHOST:1673/ABC, then the contents of the Abc.aspx page will be displayed.
Configuring information only in Application_Start is not an effective effect. The configured routing information is just a map of the routemapping.aspx page, in order to achieve a better effect, you need to routemapping.as page obtained "{controller}/{action}" information to be slightly processed.
protected voidPage_Load (Objectsender, EventArgs e) { stringController = routedata.values["Controller"] asString;//gets the controller data to the routing table//string action = routedata.values["action"] as String; if(!String.IsNullOrEmpty (Controller)) { if(Controller = ="*") {Server.Transfer ("Default.aspx");//URL address such as http://localhost:1673/Access the default page } Else { Try{Server.Transfer (Controller+". aspx");//use Server.Transfer for server end multiplicity orientation, which in turn shows the controller's corresponding page method. } Catch { } } } }
One, ASP. NET MVC route (i)---ASP. WebForm Routing Simulation