【问题标题】:MVC3 MapRoute Ambiguous between Controller Name without area and the equal Area NameMVC3 MapRoute在没有区域的控制器名称和相等的区域名称之间不明确
【发布时间】:2012-08-21 07:08:36
【问题描述】:

这是我在根目录下的控制器(无区域):

  • 首页
  • 成员

我的领域是:

+一般

  • 控制器1
  • 控制器2

+会员

  • 管理
  • 会员

所以我的Login 操作在我添加成员区域之前在成员控制器中(在根目录中),一切都很好,但知道我收到此 url 的 404 错误 (http://MyProject.dev/members/login?ReturnUrl=%2f)

那么我该如何定义一个 MapRoute 来解决这个问题呢?

更新

我在 Main Global.asax 试试这个:

routes.MapRoute(
             "newLogMaproute",
             "members/login{*pathInfo}",
             new { area = "", controller = "Members", action = "Login"}
        );

但是有错误:A path segment that contains more than one section, such as a literal section or a parameter, cannot contain a catch-all parameter.

我试试这个:

routes.MapRoute(
             "newLogMaproute",
             "members/login/{*pathInfo}",
             new { area = "", controller = "Members", action = "Login"}
        );

但是这个返回 404。

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-3 maproute


    【解决方案1】:

    这里发生的情况是区域注册发生在您的成员路由之前,因此区域路由始终优先。

    我通过在 Global.asax 中创建以下内容在测试应用中修复了此问题:

      public static void RegisterPreAreaRoutes(RouteCollection routes)
      {
         routes.MapRoute(
         "Members", // Route name
         "members/login", // URL with parameters
         new { controller = "members", action = "login" } // Parameter defaults
       );
      }
    

    然后在 Application_Start 中确保该路线被映射区域被注册之前:

      protected void Application_Start()
      {
         RegisterPreAreaRoutes(RouteTable.Routes);
         AreaRegistration.RegisterAllAreas();
         RegisterGlobalFilters( GlobalFilters.Filters );
         RegisterRoutes( RouteTable.Routes );
      }
    

    【讨论】:

      【解决方案2】:

      您需要为您所在地区定义自定义路线。 这是不好的。之后你可能会遇到问题。 最好不要产生歧义。

      不要忘记在 rotes 配置中使用 new string[] 定义命名空间:

         routes.MapRoute(
           "Members", // Route name
           "members/login", // URL with parameters
           new { }, // Parameter defaults
           new[] { "WebApp.Controllers" } // Namespaces for find 
         );
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-04-02
        • 2011-07-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-02-13
        • 1970-01-01
        相关资源
        最近更新 更多