One, ASP. NET MVC route (i)---ASP. WebForm Routing Simulation

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.