【问题标题】:Routing Issue With Areas区域的路由问题
【发布时间】:2017-04-30 07:51:15
【问题描述】:

如标题所述,我在路由和区域方面遇到问题。我的项目中有一个区域部分,其中只有一个区域。它有一个策略控制器。在项目外部有一个带有 LoginController 的控制器文件夹。

当我尝试访问 URL“BoB/Login/ChangeSection”(BoB 是项目名称,Login 是控制器,ChangeSection 是操作)时,它会被发送到“BoB/Policy/Login”。这意味着策略区域内的区域注册正在抓取请求并将策略前缀放在不应该的前面。

这是我的项目 RouteConfig

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "BoBHome", action = "Index", id = UrlParameter.Optional },
            namespaces: new string[] { "BoB.Controllers" }
            );

这是区域注册

public override void RegisterArea(AreaRegistrationContext context)
    {  
        context.MapRoute(
            name: "BoBPolicy_default",
            url: "Policy/{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
            namespaces: new string[] { "BoB.Areas.BoBPolicy.Controllers" }
        );
    }

我的印象是命名空间会阻止这种情况发生,但显然不是。任何帮助表示赞赏。

一些附加信息:

这来自另一个不是 MVC 的项目,因此重定向看起来像这样:

Response.Redirect("/BoB/BoBLogin/ChangeSection?controller=PolicyOverview");

【问题讨论】:

    标签: asp.net-mvc routes asp.net-mvc-areas


    【解决方案1】:

    我认为您的问题类似于non area route issue,需要使用 area 参数来防止在使用默认路由时插入区域 URL 前缀:

    public override void RegisterArea(AreaRegistrationContext context)
    {  
        // set area parameter with area name
        context.MapRoute(
            name: "BoBPolicy_default",
            url: "Policy/{controller}/{action}/{id}",
            // add area parameter here
            defaults: new { area = "Policy", controller = "Home", action = "Index", id = UrlParameter.Optional },
            namespaces: new string[] { "BoB.Areas.BoBPolicy.Controllers" }
        );
    }
    

    NB:由于区域路由注册总是放在默认路由之上(阅读this answer了解其背后的原因),当区域路由约束时,路由引擎可能会错误地将默认路由请求提取到区域路由中未指定。

    相关:

    MVC5 Routing Go to specific Areas view when root url of site is entered

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-03
      • 1970-01-01
      • 1970-01-01
      • 2016-08-05
      • 1970-01-01
      • 2011-11-17
      • 2021-03-03
      • 2011-07-03
      相关资源
      最近更新 更多