asp.net mvc routing extension: routing mappings

Source: Internet
Author: User

Last week I wrote three articles (one or two, iii) to introduce the ASP.net routing system in detail. Asp. NET's routing system aims to separate the request address from the file path by registering the mapping between the URL template and the physical file, but for the ASP.net MVC application, the requested target is no longer a specific physical file. Instead, you define an action method in a controller type. For the needs of its own routing characteristics, ASP. NET has extended the ASP.net routing system accordingly.

First, basic route mapping

We know from the previous introduction that a routing map based on a physical file is accomplished by invoking the Mappageroute method of the RouteTable static property routes (a RouteCollection object) that represents the global routing table. In order to achieve the routing of Target Controller and action, ASP.net MVC defines a series of extension methods for the RouteCollection type to implement a file path-independent route map. These extension methods are defined in the Routecollectionextensions type. As shown in the following code snippet, Routecollectionextensions defines two sets of methods that Ignoreroute used to register URL templates that do not need to be routed. Corresponds to the Ignore method of routecollectionextensions, as if Maproute is used for routing registrations based on URL templates, corresponding to the Mappageroute method of Routecollectionextensions.

1:public Static Class Routecollectionextensions
2: {
3://other Members
4:public static void Ignoreroute (this routecollection routes, string URL);
5:public static void Ignoreroute (this routecollection routes, string URL, object constraints);
6:
7:public static Route Maproute (this routecollection routes, string name, string URL);
8:public static Route Maproute (this routecollection routes, string name, string URL, object defaults);
9:public static Route Maproute (this routecollection routes, string name, string URL, string[] namespaces);
10:public static Route Maproute (this routecollection routes, string name, string URL, object defaults, Object Constra INTs);
11:public static Route Maproute (this routecollection routes, string name, string URL, object defaults, string[] Names paces);
12:public static Route Maproute (this routecollection routes, string name, string URL, object defaults, Object Constra INTs, string[] namespaces);
13:}

Because the routing registrations for ASP.net mvc are not related to specific physical files, the Maproute method does not have a physicalfile parameter that represents the path of the file. Unlike the Ignore and Mappageroute methods that are directly defined in Routecollectionextensions, Parameters that represent the default variable defaults and arguments based on the variable constraints of the regular expression constraints are no longer a RouteValueDictionary object, but an ordinary object. This is primarily for programmatic convenience, allowing us to specify these two parameter values in the form of anonymous types. Internally, the method obtains all the property values of the specified object by reflection, and converts to the RouteValueDictionary object, whose property name and property value are the key and value of the dictionary element.

For ASP.net mvc, it is ultimately necessary to create a specific controller instance by the controller name specified in the request address. Since the controller name corresponds only to the name of the type, the premise of the successful instantiation of controller is that we can parse its specific type correctly, so we need to use a namespace. When invoking the Maproute method we can specify a list of namespaces by using the parameter namespaces of the string array type. For a registered namespace, you can specify a string that represents the full namespace, or you can use "*" as a wildcard character.

The list of named controls added is ultimately stored in the Datatokens property of the route object, and the corresponding key is "namespaces". The Maproute method does not provide an appropriate argument for the Datatokens property of the initialization route object, if no namespace list is specified. The Datatokens property of all route objects added through this method is always an empty RouteValueDictionary object.

For a request for an action defined in a controller, if the registered routing table matches, the getroutedata of a particular routed object that matches is invoked and a specific Routedata object is returned. The name of the target controller and action, based on the resolution of the request address, must be contained in the RouteValueDictionary object corresponding to the values attribute of the Routedata. The corresponding key is controller and action.

Example Demo: Registering route mapping and viewing routing information

asp.net mvc, by defining the extension method in Routecollectionextensions Maproute for routing mapping, to give readers a deep understanding of this, let's take a simple example demo. We still use the previous scenario for getting weather information to see how the route object registered in this way returns Routedata objects for matching HTTP requests. [Source code download from here]

We are creating a asp.net Web application (not asp.net MVC application) Adds a Web page (default.aspx) and displays the related properties of Routedata directly in the way it was inline in the previous way, and the HTML for the main part of the page is shown below. Note that the routedata we are displaying are obtained from the defined method Getroutedata method, not the Routedata property corresponding to the current page.

1: <body>
2: <form id= "Form1" runat= "Server" >
3: <div>
4: <table>
5: <tr>
6: <td>Route:</td>
7: <td><%=getroutedata (). Route!= null? Getroutedata (). Route.gettype (). FullName: ""%></td>
8: </tr>
9: <tr>
: <td>RouteHandler:</td>
One: <td><%=getroutedata (). Routehandler!= null? Getroutedata (). Routehandler.gettype (). FullName: ""%></td>
</tr>
<tr>
: <td>Values:</td>
: <td>
: <ul>
<%foreach (var variable in getroutedata (). Values)
: {%>
: <li><%=variable. Key%>=<%=variable. Value%></li>
£ <%}%>
</ul>
</td>
: </tr>
: <tr>
: <td>DataTokens:</td>
: <td>
: <ul>
<%foreach (var variable in getroutedata (). Datatokens)
: {%>
A: <li><%=variable. Key%>=<%=variable. Value%></li>
: <%}%>
</ul>
: </td>
: </tr>
</table>
-</div>
Panax Notoginseng: </form>
: </body>

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.