开发者

ASP.NET routing ignoreRoute does not work

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 G开发者_StackOverflow社区etHttpHandler(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?


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())); 


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜