【问题标题】:Asp.Net MVC IgnoreRoute inside an Area区域内的 Asp.Net MVC IgnoreRoute
【发布时间】:2010-09-12 19:25:25
【问题描述】:

如何忽略区域内的路线?

我的 MVC 应用程序中有验证码。它从 GenericHanlder(.ashx) 加载它的图像,所以如果我不使用区域它工作正常,如果我只是忽略来自该 URL 的路由。

即: 在 Global.asax 中

routes.IgnoreRoute(Counts/{filename}.{ashx}/{*pathinfo});//Counts is the controller name without an area

问题是最近我将文件迁移到不同的区域并且修改了要忽略的路由的路径。现在它:

Admin/Acounts/Users/captcha.ashx?guid=3565465689789hfghfdf

所以我尝试在 Global.asax 的 routes.IgnoreRoutes 方法中更改该路径:

routes.IgnoreRoute(Admin/Acounts/Users/{filename}.ashx/{*pathinfo});

但它不再起作用了。我已经尝试在RegisterArea方法中忽略该路由,在AreaRegistrationFile中:

    context.Routes.IgnoreRoute("Admin/Accounts/Users/{filename}.ashx/{*pathinfo}");

但这也行不通。

任何想法如何忽略到区域的路线?

【问题讨论】:

  • IgnoreRoute() 用于您的 Global.asax RegisterRoutes 方法中,它并不真正知道您忽略某个区域、控制器或某个文件夹的路由的天气。它所看到的只是一个 url。
  • 我和你有同样的问题,我有两个区域它不会忽略区域管理员的路线!!!!

标签: asp.net-mvc


【解决方案1】:

IgnoreRoute 实际上只是对 Add(new Route()) 的调用,将 Route 的 RouteHandler 参数设置为新的 StopRoutingHandler。 StopRoutingHandler 告诉 URL 路由模块忽略路由并获取下一个内联 HttpHandler 的内容。

你知道路由注册对声明的顺序很敏感。我的猜测是您在其他路线已经捕获它之后声明您的 IgnoreRoute。

如果这些信息对您没有帮助,请发布您的路线注册的完整内容,因为它们将帮助我们为您提供答案。

另外,如果您使用http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx 提供的源代码来调试路由,将更容易找到问题的原因。

【讨论】:

    【解决方案2】:

    这适用于该区域的 AreaRegistration 类:

    public override void RegisterArea(AreaRegistrationContext context)
    {
        context.Routes.Add(new Route("admin/{sub}/{resource}.axd/{*pathInfo}", new StopRoutingHandler()));
        context.Routes.Add(new Route("admin/{resource}.axd/{*pathInfo}", new StopRoutingHandler()));
    
        context.MapRoute(
            "admin_default",
           "admin/{controller}/{action}/{id}",
            new { controller = "home", action = "Index", id = UrlParameter.Optional }
        );
    }
    

    这是在 MVC4 中,但它也可以在旧版本中工作。

    【讨论】:

    • 非常感谢。这正是我所需要的,因为直接在 RouteConfig 中忽略路由对我不起作用,并且context.IgnoreRoute() 不是RegisterArea 方法中的有效方法调用。
    【解决方案3】:

    对于 MVC 5,也可能是 4,RouteConfig 现在是定义默认路由的地方。在默认路由映射之前,我有很多“routes.IgnoreRoute”调用,这里有一个例子:

    routes.IgnoreRoute("{*changeaccount}", new { changeaccount = @"(.*/)?change.account(/.*)?" });
    
    routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
    

    现在,当我向站点添加设置区域时,导航到会在该区域中执行操作的 URL,然后尝试导航到 change.account,RouteConfig 中的忽略路由调用不再阻止该调用从被馈送到路由引擎。

    在阅读了这篇文章并浏览了其他地方之后,我意识到我基本上可以在我的 SettingsAreaConfiguration.cs 文件中进行相同的忽略路由调用,只是 Routes 作为 AreaRegistrationContext 的属性存在的细微差别。

    public override void RegisterArea(AreaRegistrationContext context) 
    {
        context.Routes.IgnoreRoute("{*changeaccount}", new { changeaccount = @"(.*/)?change.account(/.*)?" });
    
        context.MapRoute(
            "Settings_default",
            "Settings/{controller}/{action}/{id}",
            new { action = "Index", id = UrlParameter.Optional }
        );
    }
    

    还有哇!问题解决了!!

    现在我有很多对 IgnoreRoute 的调用,因此我将所有这些在我的站点中的标准调用复制到了一个扩展 RouteCollection 的新扩展方法。

    public static class RouteCollectionExtensions
    {
        public static void IgnoreRoutesCommon(this RouteCollection routes)
        {
            routes.IgnoreRoute("{*changeaccount}", new { changeaccount = @"(.*/)?change.account(/.*)?" });
        }
    }
    

    我可以在此方法中添加更多 IgnoreRoute 调用,因为我有一个中心位置来存储所有这些调用。

    现在,我可以将“change.account”的特定 IgnoreRoute 调用替换为对我的扩展方法的调用。

    所以 RouteConfig.cs 文件中的调用如下所示:

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoutesForCommon();
    
        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }
    

    SettingsAreaConfiguration.cs 文件中的调用如下所示:

    public override void RegisterArea(AreaRegistrationContext context) 
    {
        context.Routes.IgnoreRoutesForCommon();
    
        context.MapRoute(
            "Settings_default",
            "Settings/{controller}/{action}/{id}",
            new { action = "Index", id = UrlParameter.Optional }
        );
    }
    

    呸呸呸! :O)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-08
      • 2011-08-12
      • 2020-12-29
      • 1970-01-01
      • 2011-03-17
      • 1970-01-01
      • 2021-02-13
      • 2011-07-22
      相关资源
      最近更新 更多