Routing
— By Phil Haack
UNDERSTANDING URLS
Guidelines for high-quality URLs. You should provide:
- A domain name that is easy to remember and easy to spell
- Short URLs
- Easy-to-type URLs
- URLs that reflect(体现) the site structure
- URLs that are hackable to allow users to move to higher levels of the information architecture by hacking off the end of the URL Persistent URLs, which don’t change
INTRODUCTION TO ROUTING
Routing within the ASP.NET MVC framework serves two main purposes:
- It matches incoming requests that would not otherwise match a file on the fi le system and maps the requests to a controller action.
- It constructs outgoing URLs that correspond to controller actions.
Comparing Routing to URL Rewriting
The key difference is that URL Rewriting is focused on mapping one URL to another URL. For example, URL Rewriting is often used for mapping old sets of URLs to a new set of URLs. Contrast(对比) that to routing which is focused on mapping a URL to a resource.
Another key difference is that Routing also helps generate URLs using the same mapping rules that it uses to match incoming URLs. URL rewriting only applies to incoming requests URLs and does not help in generating the original URL.
参考:http://www.cnblogs.com/TomXu/archive/2011/12/27/2303486.html
Route Constraints
routes.MapRoute(“blog”, “{year}/{month}/{day}”
, new {controller=”blog”, action=”index”}
, new {year=@”\d{4}”, month=@”\d{2}”, day=@”\d{2}”});
If you’re familiar with regular expressions, you’d know that the regular expression \d{4} actually matches any string containing four consecutive digits such as “abc1234def.”
Routing automatically wraps the specified constraint expression with ^ and $ characters to ensure that the value exactly matches the expression. In other words, the actual regular expression used in this case is “^\d{4}$” and not \d{4} to make sure that “1234” is a match, but “abc1234def” is not.
Named Routes
public static void RegisterRoutes(RouteCollection routes) { routes.MapRoute( name: “Test”, url: “code/p/{action}/{id}”, defaults: new { controller = “Section”, action = “Index”, id = “” } ); routes.MapRoute( name: “Default”, url: “{controller}/{action}/{id}”, defaults: new { controller = “Home”, action = “Index”, id = “” } ); }
@Html.RouteLink(“Test”, new {controller=”section”, action=”Index”, id=123}) @Html.RouteLink(“Default”, new {controller=”Home”, action=”Index”, id=123})
a subtle behavior of routing
routes.MapPageRoute(“new”, “static/url”, “~/aspx/SomePage.aspx”);
When you have a route with the URL {controller}/{action}/{id}, you’re expected to supply values for controller, action, and id when generating a URL.
In this case, because the new route doesn’t have any URL parameters, it matches every URL generation attempt because technically,
“a route value is supplied for each URL parameter.” It just so happens that there aren’t any URL parameters.
That’s why all the existing URLs are broken because every attempt to generate a URL now matches this new route.
previous RouteLink Will broken:
/url?controller=section&action=Index&id=123 and /static/url?controller=Home&action=Index&id=123
Fix:
@Html.RouteLink( linkText: “route: Test”, routeName: “test”, routeValues: new {controller=”section”, action=”Index”, id=123} ) @Html.RouteLink( linkText: “route: Default”, routeName: “default”, routeValues: new {controller=”Home”, action=”Index”, id=123} )
Use names for all your routes and always use the route name when generating URLs. Most of the time, letting Routing sort out which route you want to use to generate a URL.
MVC Areas
routes.MapRoute( “Default”, “{controller}/{action}/{id}”, new { controller = “Home”, action = “Index”, id = “” }, new [] { “AreasDemoWeb.Controllers” } );
Catch-All Parameter
A catch- all parameter allows for a route to match a URL with an arbitrary(任意) number of segments(段).
public static void RegisterRoutes(RouteCollection routes) { routes.MapRoute(“catchallroute”, “query/{query-name}/{*extrastuff}”); }
| URL | PARAMETER VALUE |
| /query/select/a/b/c | extrastuff = “a/b/c” |
| /query/select/a/b/c/ | extrastuff = “a/b/c” |
| /query/select/ |
extrastuff = “” (Route still matches. The |
StopRoutingHandler and IgnoreRoute
By default, routing ignores requests that map to physical files on disk. That’s why requests for fi les
such as CSS, JPG, and JS fi les are ignored by routing and handled in the normal manner(方式).
public static void RegisterRoutes(RouteCollection routes) { routes.Add(new Route ( “{resource}.axd/{*pathInfo}”, new StopRoutingHandler() )); routes.Add(new Route ( “reports/{year}/{month}” , new SomeRouteHandler() )); }
If a request for /WebResource.axd comes in, it will match that fi rst route. Because the fi rst route returns a StopRoutingHandler, the routing system will pass the request on to normal ASP.NET processing.
An Easier way to tell routing to ignore a route:
public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute(“{resource}.axd/{*pathInfo}”); routes.MapRoute(“report-route”, “reports/{year}/{month}”); }
Debugging Routes
RouteDebugger
use NuGet to install it via the following command, Install-Package RouteDebugger.
This package adds the RouteDebugger assembly and adds a setting to the appSettings section of web.confi g used to turn route debugging on or off:
<add key=”RouteDebugger:Enabled” value=”true” />
Wait End..
浙公网安备 33010602011771号