When discussing ASP. net mvc routing, I did not intend to find that ASP. NET had a routing mechanism. When learning MVC routing, I feel that this part of the information is not too much and is not very full (maybe I don't know how to go to Microsoft's official documentation ). Later, I tried ASP. NET routing. This article also reads the reading notes written by Jiang Jinnan and chongdian!
One of the most notable effects of the routing mechanism is the separation of URLs and physical files. This separation has three advantages: more flexible, better readability, and more SEO-friendly.
Specifically, the flexibility is that the file path has been changed (for example, put in a new folder), so you have to change all the URLs involved in that file, if you are a little lazy, press Ctrl + H. If route ing is used, you only need to modify it in one place, which is simple and easy to understand. A better way is that when a traditional URL transmits parameters, it will always question "?" [Parameter name] = [parameter value] will be connected one by one, just like this
Http: // 127.0.0.1: 8083/WebForm1.aspx? Param1 = parameterValue1 & param2 = parameterValue2
However, the URL under the routing mechanism becomes concise and clear.
Http: // 127.0.0.1: 8083/WebForm1/parameterValue1/parameterValue2 (the routing mode is not mentioned at the moment );
I cannot give an example of SEO friendliness!
The following is a simple example to demonstrate how to use this render machine to separate URLs from physical files.
In the Global. asax file of the MVC project, the routing definition is put in a static method of RegisterRoutes (RouteCollection routes), which is called in Application_Start. Similar to ASP. NET, the routing definition must be completed in Application_Start (). The Code is as follows:
Application_Start(= RouteValueDictionary { { , }, { , , , ,
Here, the main route adding method is to call the MapPageRoute method. The parameters in the method are similar to those of adding a route under MVC, including the route name, route mode, and physical file name, here I have defined the default values for both parameters. Both param1 and param2 are "*". In fact, other constraints can be defined. For example, the parameter value must comply with certain format requirements, for other reloads of the MapPageRoute method, the following lists
Route MapPageRoute( routeName, routeUrl, Route MapPageRoute( routeName, routeUrl, physicalFile, Route MapPageRoute( routeName, routeUrl, physicalFile, Route MapPageRoute( routeName, routeUrl, physicalFile, Route MapPageRoute( routeName, routeUrl, physicalFile, checkPhysicalUrlAccess, RouteValueDictionary defaults, RouteValueDictionary constraints, RouteValueDictionary dataTokens);
In this example, You Need An aspx page located in the root directory named WebForm1.aspx. The load event binding method is as follows:
Page_Load(= temp=.RouteData.Values[, temp==?= .RouteData.Values[, temp == ?
After the URL is generated, enter different URLs as follows:
In fact, this route is not only added through the MapPageRoute method, but also through the implementation of the IHttpHandler interface and IRouteHandler. Let's start from the beginning. The Routes attribute of RouteTable is actually a RouteCollection instance, because it is a collection type, you can call its Add method to Add an instance with a Route. This Route instance includes the URL mode and some processing rules (including the physical file corresponding to the request .)
The class diagram involved is as follows:
Two classes are defined here. One is MyHttpHandler, which implements the IHttpHandler interface and the other is MyRouteHandler, which implements the IRouteHandler interface. The Route constructor can pass in an IRouteHandler parameter, and the MyRouteHandler class defined by itself is used when passing the parameter. The IRouteHandler member has a GetHttpHandler (RequestContext requestContext) method. This method is used to obtain an instance of the class implementing IHttpHandler. Here, the returned result is a custom MyHttpHandler instance. There is a virtual void ProcessRequest (HttpContext context) virtual method in the MyHttpHandler class. This virtual method is used to request a processing program in a virtual path.
Here we stick to the Code, mainly the definition of two classes.
RequestContext {;. RequestContext = IHttpHandler member context. Server. Execute (+ RequestContext. RouteData. Values [{IRouteHandler member of MyHttpHandler class}MyRouteHandler class
You only need to add a line of code in Application_Start (object sender, EventArgs e ).
RouteTable.Routes.Add( Route(, MyRouteHandler()));
The WebForm1.aspx Code does not need to be changed. http: // localhost: 1144/122343/abcdef/WebForm1.aspx is used to send a request. The result is the same.
However, the parameter here cannot be blank, so the default value is not set.
During the reload of the MapPageRoute method listed above, we also found that the parameter above the URL can set the default value for the parameter and restrict the format of the parameter. Next we will try again. You must use the RouteValueDictionary class for both the default value and format constraints.
Application_Start(= RouteValueDictionary { { , }, { , } }; RouteValueDictionary constraint = RouteValueDictionary { { , } }; RouteTable.Routes.MapPageRoute(, , ,
The URLs and results are listed as follows:
Request URL |
Result |
|
Param1: 0 Param2: 0 |
| |
Param1: sdfb Param2: 0 |
| |
Param1: sdfb Param2: 343 |
| |
Resource cannot be found due to HTTP 404 error. |
If you use the IHttpHandler interface and IRouteHandler, you need to make some changes in the Application_Start method.
Application_Start(= RouteValueDictionary { { , }, { , = RouteValueDictionary { { , Route(,defaultParam,constraint,
The results are the same as those in the preceding table.
In this case, if the MapPageRoute method is used in the preceding route settings, the specified page can still be accessed if the request is made according to the previous file path. For example, WebForm1.aspx, which has been used in the preceding process, can be accessed by following the http: // localhost: 1144/WebForm1.aspx URL request, however, the difference is that our route does not process it. The default value set in the route does not take effect at all. The result is as follows:
If you want to route this URL, you need to set an attribute
RouteTable.Routes.RouteExistingFiles = ;
As the name suggests, it indicates whether to route an existing file. It is actually an attribute of the RouteCollection class. It is a global attribute for the route of the entire site. The default value is false. When it is set to true, the request is sent back to http: // localhost: 1144/WebForm1.aspx. The result is as follows:
That is to say, the route takes effect. WebForm1.aspx is regarded as parameter 1 rather than the file name of a physical file. 0 is the default value of parameter 2.
Now all URLs are routed, so js, css, images, and other file references will be routed. For example, add this
The result is only as follows:
In this case, you need to cancel routing for some files,
RouteTable.Routes.Ignore();
The picture will be available.
This method can only be used to cancel one type of files, set js files, set css files, set png files, and set gif files ...... But MVC is Ignore, and this kind of "{resource}. axd/{* pathInfo}" is enough. This is what it is like here.
RouteTable.Routes.Ignore();
But I lost again, why ????
This Routing Mechanism also has another purpose, that is, to construct a new URL Based on the Route. This construction mainly uses the GetVirtualPath method, which is available in RouteCollection and Route. The difference is that when you call the RouteCollection's GetVirtualPath, it will traverse all the Route objects in the entire set and call the GetVirtualPath method of the Route object one by one until the returned value is not null, if it is always null, only null is returned.
When we define such a route
RouteValueDictionary defaultParam = RouteValueDictionary { { , }, { , = RouteValueDictionary { { , , , , , defaultParam, constraint);
Add the following code in WebForm1.aspx's Load time binding method:
RouteData routeData = , , = , , , ).VirtualPath + ).VirtualPath + + );
When we request http: // localhost: 1144/abc/123, the three URLs are
From the code above, we can see that the first call did not transmit RequestContext, nor provided routing parameters. The obtained URL is "/". The second call only transmits the current RequestContext, no route parameters are provided. The obtained URL is "/abc/123", the third request context and route parameters are passed, and the route parameter is param1 and abc, param2 is 123, and the obtained URL is "/efg/456 ". It can be seen that when a route parameter is passed in, the generated URL must be generated according to the route parameter; when no route parameter is passed and only RequestContext is passed, the generated URL is generated according to the Routing Parameters of RequestContext. when nothing is passed, only URLs with null parameters can be generated. That is, for URL generation, the routing parameter has a higher priority than RequestContext.
In fact, I still don't know what the purpose of constructing a URL is. Remember to leave an impression first, and then you can leave a background in case of use.
This article was actually drafted two months ago. It was put on hold for various reasons and now I want to write it again. I once thought about writing a series of articles about APS. net mvc. Unfortunately, I know little about it and can write less about it. There must be many shortcomings in this article. I hope you can give me more advice. Thank you!
Finally, I will attach some useful articles.
This article comes from most of the content listed above. If you offend me, I will withdraw this article from the garden homepage!