ASP. NET MVC is a highly extensible framework that can be extended from URL requests until the final HTML rendering, so it is also necessary to understand how the framework works and recommend Artech.
What we recall today is not the filter in MVC, the activation of the controller or the execution of the action, or the generation of the URL route routedata, which we recall is the Routetable.routes, the two properties of the global routing table. Appendtrailingslash and Lowercaseurls.
The function of Appendtrailingslash is whether to add/slash at the end of the generated URL (if none exists). Set to True, a slash is added at the end of the generated URL, otherwise it is not added automatically .
The role of Lowercaseurls is to convert the generated URLs to lowercase. Because for search engines, the case of URL addresses may be treated differently.
These two properties act on the generated URL, which is obtained through RouteTable.Routes.GetVirtualPath () through the incoming request context RequestContext.
Maybe you would say, if I want to implement the generated URL lowercase, then I'll set Lowercaseurls to True, yes, is that really the case? Let's do an experiment.
Registered URL routes:
1 Public classRouteconfig2 {3 Public Static voidregisterroutes (routecollection routes)4 {5Routes. Ignoreroute ("{Resource}.axd/{*pathinfo}");6Routes. Appendtrailingslash =true;//whether to add slash delimiter7Routes. Lowercaseurls =true;//url whether lowercase8 routes. MapRoute (9Name"Default",TenUrl:"{Controller}/{action}/{id}", OneDefaultsNew{controller ="Home", action ="Index", id =urlparameter.optional}, ANamespaces:New string[] {"mvcapplication3.controllers" } - ); - } +}
The corresponding action method:
1 [ActionName ( " Span style= "Color:rgb (128, 0, 0); >list " )] public ActionResult List () 3 { 4 viewbag.employees = repository. Employeerepository.getemployees ( )/////is to get a list of several 5 retu RN View (); 6 }
The corresponding view:
1 @{2Layout =NULL;3 }4 5<! DOCTYPE html>6 789<meta name="Viewport"Content="Width=device-width"/>Ten<title>List</title> One A<body> -<div> - @{ thelist<mvcapplication3.models.employee> Listemployee = viewbag.employees asList<mvcapplication3.models.employee>; - } - @{ - if(Listemployee! =NULL) + { -<ul> + @{ A at foreach(varIteminchListemployee) - { -<li> -@Html. RouteLink ("RouteLink"+item. Name,"Default",New{action ="Detail", CONTROLLER ="Home", id =item. ID}) -<br/> -@Html. ActionLink ("ActionLink"+item. Name,"DETAIL","Home",New{id = Item.} Id},New{target ="_blank" }) in</li> - } to } +</ul> - } the Else * { $ Panax NotoginsengNo data display in <div> list </div> - } the } +</div> A</body> theWe generate two hyperlinks in the view through Html.routelink and Html.ationlink respectively. What is the result of the execution?
1 <Li>2 <ahref= "/home/detail/0/">RouteLinkguozhiqi0</a> <!--This is the url--> generated by RouteLink3 <BR/>4 <ahref= "/home/detail/0"Target= "_blank">ActionLinkguozhiqi0</a><!--This is the url--> generated by ActionLink5 </Li>
From the above results we can see a strange phenomenon, that is, the URL generated by Html.routelink () applied the properties we set, that is, the URL address global lowercase, and the end of a slash delimiter.
But ActionLink did not add a slash at the end, nor did it all lowercase. What is this for? If anyone knows, please tell me the exact difference. Thank you.
So if we want to generate a URL address with all lowercase and a slash delimiter at the end, use RouteLink or rewrite the route's getvirtualpathdata () to lowercase all uppercase letters.