ASP.NET routing ignoreRoute does not work

3.4k views Asked by At

I can't exclude non-existing files from the routing system. I am dealing with this code in a Web Forms scenario:

public static void RegisterRoutes(RouteCollection routes)
{   
  routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
  routes.IgnoreRoute("{resource}.gif/{*pathInfo}");
  routes.IgnoreRoute("{resource}.jpg/{*pathInfo}");   
  Route r = new Route("{*url}", new MyRouteHandler());
  routes.Add(r);
}

When I debug

public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
    string path;

    IHttpHandler page;

    try
    {
        path = requestContext.RouteData.GetRequiredString("url");
        LogFile(requestContext, path);
    }

path still contains non existing gif files, jpg etc I want to exclude all files that have an extension if that’s possible

Is something wrong with the code above? Is the order correct, i.e. add routes.IgnoreRoute entry prior to adding a route to RouteCollections?

2

There are 2 answers

0
James Lawruk On

In Web Forms, you can use the StopRoutingHandler. In the following example, routing will be ignored for files in the /images folder like http://yoursite.com/images/xyz.jpg

routes.Add(new Route("images/{resource}", new StopRoutingHandler())); 
0
RPM1984 On

IgnoreRoute is an extension method of ASP.NET MVC (System.Web.Mvc) - does not work in Web Forms.

Do this:

routes.Add(new Route("{resource}.gif/{*pathInfo}", new MyIgnoreHandler()));

Map your other routes to your regular handler.

You should remove the "mvc" tag from this question.