【问题标题】:ASP.NET routing ignoreRoute does not workASP.NET 路由 ignoreRoute 不起作用
【发布时间】:2011-03-10 21:45:15
【问题描述】:

我无法从路由系统中排除不存在的文件。我在 Web 窗体方案中处理此代码:

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

当我调试时

public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
    string path;

    IHttpHandler page;

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

路径仍然包含不存在的 gif 文件、jpg 等 如果可能的话,我想排除所有具有扩展名的文件

上面的代码有问题吗?顺序是否正确,即在向 RouteCollections 添加路由之前添加 routes.IgnoreRoute 条目?

【问题讨论】:

  • 我下面的答案不走运?

标签: asp.net routing ignoreroute


【解决方案1】:

在 Web 表单中,您可以使用 StopRoutingHandler。在以下示例中,将忽略 /images 文件夹中的文件(如 http://yoursite.com/images/xyz.jpg)的路由

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

【讨论】:

    【解决方案2】:

    IgnoreRoute 是 ASP.NET MVC 的扩展方法(System.Web.Mvc) - 在 Web 窗体中不起作用。

    这样做:

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

    将您的其他路线映射到您的常规处理程序。

    你应该从这个问题中删除“mvc”标签。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-15
      • 2013-02-23
      相关资源
      最近更新 更多