Notes on learning ASP. NET MVC5 framework-ASP. net mvc routing (2)

Source: Internet
Author: User

Notes on learning ASP. NET MVC5 framework-ASP. net mvc routing (2)
2.2.2 route Registration

ASP. net mvc registers a route by calling the extension method MapRoute of the RouteCollection object representing the global route table. Let's take a simple example. We still use the previous routing template for obtaining weather information to see what kind of RouteData object will be returned for matching requests registered using this method.

We create an empty ASP. NET Web program, and manually add "System. Web. Mvc. dll" and "System. Web. WebPages. Razor. dll" references. Add the next page (Default. aspx) as shown in the following figure, and display the RouteData attributes in the internal join mode according to the previous practice. The displayed RouteData is obtained by calling the custom GetRouteData method, rather than the RouteData object returned by the RouteData attribute on the current page.

 

 

The GetRouteData method is defined in the background code of the current page. As shown in the following code snippet, an HttpContext object is created based on the manually created HttpRequest (the request URL is "http: // localhost/0512/3") and HttpResponse object, create an HttpContextWrapper object. Next, we use the static RouteTable attribute Routes to obtain the RouteCollection object representing the global route table, and call the HttpContextWrapper object as a parameter to call its GetRouteData method. This method is used to simulate the route resolution of the registered route table for requests whose relative address is "/0512/3.

 

 

ublic partial class Default : System.Web.UI.Page    {        private RouteData routeData;        public RouteData GetRouteData()        {            if (null != routeData)            {                return routeData;            }            HttpRequest request = new HttpRequest("default.aspx", "http://localhost/0512/3", null);            HttpResponse response = new HttpResponse(new StringWriter());            HttpContext context = new HttpContext(request, response);            HttpContextBase contextWrapper = new HttpContextWrapper(context);            return routeData = RouteTable.Routes.GetRouteData(contextWrapper);        }    }


 

The specific route registration is still defined in the added Global. asax file. As shown in the following code, RouteCollection Objects representing the global route table are obtained using Routes, the static attribute of RouteTable, then, call its MapRoute method to register a Route object using "{areacode}/{days}" as the routing template, and specify the default values, constraints, and namespace lists of variables. Because the successfully matched route object must have a routing variable named "controller", we directly set the default value of controller to "home ".

 

public class Global : System.Web.HttpApplication    {        protected void Application_Start(object sender, EventArgs e)        {            object defaults = new            {                areacode = "010",                days = 2,                defaultCity = "BeiJing",                defaultDays = 2,                controller = "Home"            };            object constraints = new { areacode = @"0\d{2,3}", days = @"[1-3]" };            string[] namespaces = new string[] { "Artech.Web.Mvc", "Artech.Web.Mvc.Html" };            RouteTable.Routes.MapRoute("default", "{areacode}/{days}",defaults, constraints, namespaces);        }    }


 

Related Article

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.