Learning ASP. NET MVC5 framework secrets note-ASP. NET routing (12), mvc5-asp.net
2.1.5 generate a URL Based on routing rules
ASP. NET routing system has two main applications: one is to register the routing template and the physical file ing to achieve the separation of request URL and physical address, the other is to generate a complete URL through the registered routing rules, which is implemented by calling the GetVirtualPath method of the RouteCollection object.
As shown in the following code snippet, RouteCollection defines two GetVirtualPath method overloading. Their common parameters requestContext and values represent the request context (RouteData and HTTP context encapsulation) and the routing variable used to replace the variable placeholder defined in the routing template. The other GetVirtualPath method has an additional string parameter name, which indicates the Registration name of the specific route object used in the collection (the first parameter specified when the MapPageRoute method is called ).
public VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values);public VirtualPathData GetVirtualPath(RequestContext requestContext, string name, RouteValueDictionary values);
If the GetVirtualPath method is called without specifying the Route object that generates the virtual path, the method traverses the entire Route table until a Route template matches the specified Route parameter list, and returns the VirtualPathData object generated by it. Specifically, this method calls the GetVirtualPath method of each Route object in the Route table sequentially until the method returns a specific VirtualPathData object. If the return value of the GetVirtualPath method of all Route objects is Null, the return value of the entire method is also Null.
When calling the GetVirtualPath method, you can pass Null as the first parameter (requestContext). In this case, it will be based on the Current HTTP context (corresponding to the Current static attribute of HttpContext) create a RequestContext object as a parameter for calling the Route object GetVirtualPath method. The parameter of this method contains an empty RouteData object. If the current HTTP context does not exist, this method will directly throw an exception of the InvalidOperationException type.
Route parsing of the Route object for the GetVirtualPath method only requires that the value of the variables defined in the routing template can be provided, and these variable values have three sources, the default value defined for the Route variable in the Route object and the value of the variable (Values attribute) provided in RouteData of the specified RequestContext object) and additional variable values (RouteValueDictionary object specified through the values parameter), these three variable values have a low-to-high priority.
Take the previously defined routing template for Weather information as an example. in the background code of the aspx page, use the static Routes of RouteTable to obtain the RouteCollection object representing the global route table, and call its GetVirtualPath method to generate three specific URLs.
public partial class Weather : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { RouteData routeData = new RouteData(); routeData.Values.Add("areaCode", "0512"); routeData.Values.Add("days", "1"); RequestContext requestContext = new RequestContext(); requestContext.HttpContext = new HttpContextWrapper(HttpContext.Current); requestContext.RouteData = routeData; RouteValueDictionary values = new RouteValueDictionary(); values.Add("areaCode", "028"); values.Add("days", "3"); Response.Write(RouteTable.Routes.GetVirtualPath(null, null).VirtualPath+ "<br/>"); Response.Write(RouteTable.Routes.GetVirtualPath(requestContext,null).VirtualPath + "<br/>"); Response.Write(RouteTable.Routes.GetVirtualPath(requestContext,values).VirtualPath + "<br/>"); } }
From the code snippet above, we can see that the requestContext and values parameters passed in by calling the GetVirtualPath method for the first time are both Null. The second time, a manually created RequestContext object is specified, its RouteData's Values attribute has two variables (areaCode = 0512; days = 1), while the values parameter is still Null. For the third time, a specific object is specified for the requestContext and values parameters at the same time, the latter contains two parameters (areaCode = 028; days = 3 ).