ASP.NET MVC Routing – StopRoutingHandler and IgnoreRoute

There are two methods to exclude a URL from being handled by Routing – StopRoutingHandler and IgnoreRoute.

StopRoutingHandler

The below listing shows two routes created manually, the first of which has a StopRoutingHandler which blocks Routing for handling the matching URL:

public static void RegisterRoutes(RouteCollection routes)
{
routes.Add(new Route
(
"{appresource}.axd/{*urlInfo}", new StopRoutingHandler() ));

routes.Add(new Route
(
"articles/{cat}/{id}", new SomeRouteHandler()  ));
}

Therefore a URL such as resource1.axd will be matched to the first route and since that route returns a StopRoutingHandler  Routing system will pass this request on for normal ASP.NET processing.

IgnoreRoute

A simpler way to circumvent Routing is to use IgnoreRoute .

This is declared in a similar manner to the familiar MapRoute method and is normally used alongside it as shown below:

public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute(“{appresource}.axd/{*urlInfo}“);
routes.MapRoute("norm", “articles/{cat}/{id}“, new MvcRouteHandler());
}

This is a lot tidier than the method using StopRoutingHandler and functions exactly the same.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>