2

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

  • 成员

我的领域是:

+一般

  • 控制器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。

4

2 回答 2

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 );
  }
于 2012-08-26T15:32:46.527 回答
0

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

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

   routes.MapRoute(
     "Members", // Route name
     "members/login", // URL with parameters
     new { }, // Parameter defaults
     new[] { "WebApp.Controllers" } // Namespaces for find 
   );
于 2014-05-07T20:13:26.470 回答