In general, we can get an application based global routing table through the static properties of routetable routes, which we know is a kind of routecollection collection object. We can route the map by calling its Mappageroute, which is the matching relationship between the registered URL template and a physical file. The core of route registration is to add a route object to the Global routing table, where most of the properties can be specified by the Mappageroute method's related parameters. Next we illustrate some of the details of routing registration by implementing the demo.
We have previously described the routing address for getting weather information, we created a weather.aspx page in the ASP.net Web application we created, but we do not intend to present any weather information on the page, but instead print out the routing information based on that page. The HTML for the main part of the page is shown below, and we will display not only the route and Routehandler property types of the Routedata object based on the current page, but also the variables stored in values and Datatokens dictionaries.
1: <body>
2: <form id= "Form1" runat= "Server" >
3: <div>
4: <table>
5: <tr>
6: <td>Route:</td>
7: <td><%=routedata.route!= null? RouteData.Route.GetType (). FullName: ""%></td>
8: </tr>
9: <tr>
: <td>RouteHandler:</td>
One: <td><%=routedata.routehandler!= null? RouteData.RouteHandler.GetType (). FullName: ""%></td>
</tr>
<tr>
: <td>Values:</td>
: <td>
: <ul>
<%foreach (var variable in routedata.values)
: {%>
: <li><%=variable. Key%>=<%=variable. Value%></li>
£ <%}%>
</ul>
</td>
: </tr>
: <tr>
: <td>DataTokens:</td>
: <td>
: <ul>
<%foreach (var variable in routedata.datatokens)
: {%>
A: <li><%=variable. Key%>=<%=variable. Value%></li>
: <%}%>
</ul>
: </td>
: </tr>
</table>
-</div>
Panax Notoginseng: </form>
: </body>
In the added Global.asax file, we define the route registration operation in the Application_Start method. As shown in the following code snippet, the URL template mapped to the Weather.aspx page is {areacode}/{days}. When invoking the Mappageroute method, we also define the default values and regular expressions for the two variables defined in the URL template. In addition, we have appended two variables to the registered routing object, indicating a description of the default value of the variable (defaultcity:beijing;defaultdays:2).
1:public class Global:System.Web.HttpApplication
2: {
3: protected void Application_Start (object sender, EventArgs e)
4: {
5: var defaults = new RouteValueDictionary {{"AreaCode", "010"}, {"Days", 2}};
6: var constaints = new RouteValueDictionary {{"AreaCode", @ "0\d{2,3}"}, {"Days", @ "[1-3]{1}"}};
7: var datatokens = new RouteValueDictionary {{"defaultcity", "BeiJing"}, {"Defaultdays", 2}};
8:
9: RouteTable.Routes.MapPageRoute ("default", "{areacode}/{days}", "~/weather.aspx", false, defaults, Constaints, Datatokens);
: }
11:}
One, variable default value
Since we define the default value (AREACODE:010;DAYS:2) for variables that define the area code and days in the URL template, if we want to return to the weather in Beijing for the next two days, we can directly access the application root address, or you can specify only the specific area code, or both the area code and the number of days. As shown in the following figure, when we enter the above three different URLs in the browser address bar, we get the same output.
From the routing information shown in the following illustration, we can see that by default Routedata's route property type is route, and Routehandler property is a Pageroutehandler object. We will provide a detailed introduction to Pageroutehandler in the following sections of this chapter. Variables resolved through addresses are stored in the values attribute, while the variables defined for the route object Datatokens property in the route registration process are moved to the Routedata attribute of the same name. [Instance source code Download]