Register the Route first
routes. MapRoute (Name:"Member", URL:"Member/{action}/{page}", defaults:New{controller ="Membercenter", action ="List", id =urlparameter.optional}, Constraints:New{action ="Index|list|detail", page ="\d+" } ); Routes. MapRoute (Name:"Default", URL:"{Controller}/{action}/{id}", defaults:New{controller ="Home", action ="Index", id =urlparameter.optional});
Note that the routing constraints are exactly matched, for example: page = "\d+", and the actual execution is converted to: page = "^\d+$" to match.
Add the following content test to the view file:
<p> @RouteTable. Routes.getvirtualpath ( request.requestcontext, new RouteValueDictionary (new { "membercenter", " Detail " }) ). virtualpath; </p>
This can only match the second one (the first one requires the page parameter):/membercenter/detail;
This time add a test with the page parameter:
RouteTable.Routes.GetVirtualPath (Request.requestcontext, new routevaluedictionary (new Span style= "color: #000000;" > {Controller = " membercenter , Action = " = " test
/membercenter/detail?page=test
It is important to note that if you change the parameter of the second registration to page = Urlparameter.optional will cause the route resolution overlay overlay, parse the result:
/membercenter/detail; /membercenter/detail/test is the second one after the registration.
If you add a link: @Html. ActionLink ("Test link", "detail", "Membercenter", new {page = "testpage"}, NULL); Always point to Http://localhost:44622/MemberCenter/detail/testpage
MVC Routing Test