Programming asp

Page 320

Ignoring Routes In addition to defining routes that map to controllers and actions, ASP.NET MVC also allows you to define routes with URL patterns that it should simply ignore. The same RoutesTable object that exposes the MapRoute() method also exposes an Ignore Route() method, which adds a special route that tells the routing engine to ignore requests for any URL that matches a given pattern. Consider the following snippet from the default routing logic that Visual Studio generates, which instructs ASP.NET MVC to ignore routes that contain .axd, the file extension used for common ASP.NET handlers such as Trace.axd and WebResource.axd: routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

With this call to IgnoreRoute() in place, requests for these URLs are handled as normal requests to ASP.NET instead of being handled by the routing engine. You can use the IgnoreRoute() method to ignore other requests as well. For instance, consider a scenario in which a section of your website contains code written in another framework or language that should not be handled by the ASP.NET MVC runtime. In this case you might use something like the following snippet to tell ASP.NET MVC to ignore URLs that start with php-app: routes.IgnoreRoute("php-app/{*pathInfo}");

Note that if you are going to use the IgnoreRoute() method in your application, it’s important to place calls to this method before adding standard routes via the Map Route() method.

Catch-All Routes Another feature of the URL parsing engine in ASP.NET MVC is the ability to specify a “catch-all placeholder.” Catch-all placeholders are created by placing an asterisk (*) character at the beginning of the placeholder and can only be included as the last segment of a route. In fact, you saw an example of the catch-all placeholder in action in the previous phpapp example: routes.IgnoreRoute("php-app/{*pathInfo}");

Catch-all placeholders can also be used in normal mapped routes. For example, you can use a catch-all placeholder in a search scenario to gather raw search terms: routes.MapRoute( "Default", "{controller}/{action}/{*queryValues}", new { controller = "Store", action = "Search" } );

302 | Chapter 14: Advanced Routing

www.it-ebooks.info


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.