Learn the ASP. MVC5 Framework Secret Note-asp.net MVC Routing (ii)

Source: Internet
Author: User

2.2.2Route Registration

ASP. NET MVC routes registrations by calling the extension method MapRoute , which represents the global routing table's routecollection object . Let's take a simple example demo. We still follow the previous route template for getting weather information and see How a Routedata object is returned for a matching request by a route object registered in this way .

We create an emptyASP . NET Webprogram, and manually add theSystem.Web.Mvc.dll"and"System.Web.WebPages.Razor.dll"Reference. Then add one of the following pages (Default.aspx),In a way that is inline with the previous practice and directlyRoutedatarelated properties are displayed. We show theRoutedatais by calling the customGetroutedatamethod to get, not the current page.Routedataproperty returns theRoutedataobject.

<form id= "Form1" runat= "Server" > <div> <table class= "Table table-bordered" > <tr> <td>Route:</td> <td><%=getroutedata (). Route! = null? Getroutedata (). Route.gettype (). FullName: "%></td> </tr> <tr> <td>routehandle R:</td> <td><%=getroutedata (). Routehandler! = null? Getroutedata (). Routehandler.gettype (). FullName: "%></td> </tr> <tr> <td>Values:< /TD> <td> <ul> <%foreach (var vari Able in getroutedata (). Values) {%> <li> <% =variable. Key%>=<%=variable.                             Value%></li><%}%> </ul> </td> </tr> & lt;tr> <td>DataTokens:</td> <td> <ul&                             Gt <%foreach (var variable in getroutedata (). Datatokens) {%> <li> &L T;%=variable. Key%>=<%=variable. value%></li> <%}%> </ul> </ td> </tr> </table> </div> </form>

We willGetroutedatamethod is defined in the background code of the current page. As shown in the code snippet below, we have created a manual based on theHttpRequest (RequestURLto "HTTP://LOCALHOST/0512/3")and theHttpResponseobject creates aHttpContextobject, and then create aHttpcontextwrapperobject. Next we useroutetablethe static propertyRoutesthat represents the global routing table.routecollectionobject, and put thisHttpcontextwrapperobject is called as a parameter .Getroutedatamethod. This method is actually a simulation of the registered routing table for the relative address of "/0512/3"The routing resolution for the request.

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 addedGlobal.asaxfile. As shown in the following code, we use theroutetablethe static propertyRoutesthat represents the global routing table.routecollectionobject, and then calls itsMapRoutemethod registers a use of the{areacode}/{days}"as a route template.Routeobject and specifies the default values, constraints, and namespace lists for the variables. Because the routing object that successfully matches must have a name of "Controller"of the routing variables, we will directlyControllerThe default value is set 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);        }    }


Learn the ASP. MVC5 Framework Secret Note-asp.net MVC Routing (ii)

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.