2011-04-17 91 views
7

我已经为我的MVC 3项目添加了一个区域。我似乎无法使用一个非常简单的方案进行路由。它似乎总是想解决该地区。这是我的配置。在启动时:MVC区域 - 非区域路径解决到区域

AreaRegistration.RegisterAllAreas(); 
IgnoreRoute("{resource}.axd/{*pathInfo}"); 
routes.MapRoute(
    "Default", 
    "{controller}/{action}/{id}", 
    new { controller = "Browse", action = "Index", id = UrlParameter.Optional } 

而且

public class AdminAreaRegistration : AreaRegistration 
{ 
    public override string AreaName 
    { 
     get { return "Admin"; } 
    } 

    public override void RegisterArea(AreaRegistrationContext context) 
    { 
     context.MapRoute(
      "Admin_default", 
      "Admin/{controller}/{action}/{id}", 
      new { controller = "Users", action = "Index", id = UrlParameter.Optional } 
     ); 
    } 
} 

在web.config中:

<authentication mode="Forms"> 
    <forms loginUrl="~/Login" defaultUrl="~/Browse" timeout="60" cookieless="UseDeviceProfile" /> 
</authentication> 

我使用RouteDebugger来尝试解决这个问题。当我浏览到登录页面的调试器显示:

  • AppRelativeCurrentExecutionFilePath:〜登录
  • 管理/ {控制器}/{行动}/{ID} 不匹配当前请求
  • {控制器}/{行动}/{ID} 匹配当前请求
  • 匹配的路由:{控制器}/{行动}/{ID}

到目前为止去OD。但是,就说明这一点:

  • 生成的URL:/管理/登入RETURNURL =%2F使用route “管理/ {控制器}/{行动}/{ID}”

下一页我登录我的登录/索引方法没有击中,调试器显示:

  • AppRelativeCurrentExecutionFilePath:〜登录
  • 管理/ {控制器}/{行动}/{ID} 不匹配当前请求
  • {控制器}/{行动}/{ID} 匹配当前请求
  • 匹配的路由:{控制器}/{行动}/{ID}
  • 生成的URL:/管理/登入RETURNURL =% 2FAdmin%2FLogin使用

一方面,它说,它不符合管理的路线,然后在生成的URL它说,它的路径“管理/ {控制器}/{行动}/{ID}”使用该路线。我很难过。

回答

3

尝试使用预定义的价值,你的路由定义添加您所在地区的参数...例如,而不是:

context.MapRoute(
      "Admin_default", 
      "Admin/{controller}/{action}/{id}", 
      new { controller = "Users", action = "Index", id = UrlParameter.Optional } 
     ); 

使用:

context.MapRoute(
      "Admin_default", 
      "Admin/{controller}/{action}/{id}", 
      new { area = "Admin", controller = "Users", action = "Index", id = UrlParameter.Optional } 
     ); 

让我知道,如果有帮助.. Regards

+0

我的确如你所说。首先,我尝试在非区域注册之前进行区域注册。不好。与非区域控制器的链接始终通过前面的“Admin /”解决。所以我扭转了注册顺序。然后,当我导航到“〜/ Admin”时,调试器显示“{controller}/{action}/{id}”路径上的匹配,从而得到“〜/ Home/NotFound”。当我导航到“〜/ Admin/Users”时,我得到了“〜/ Admin/Home/Home/Home/NotFound”,调试器在{catchall}路径上显示匹配。 – 2011-05-05 21:09:14

+0

当我说:“非区域控制器的链接总是以'Admin /'在他们面前解决”这不完全正确。 Html.ActionLink 正确解析。但是,即使T是不在区域中的控制器,Html.BeginForm 和Html.BuildUrlFromExpression 也会解析为管理区域。 – 2011-05-05 21:30:36

+0

我使用的是Microsoft.Web.Mvc的2.0版本,其中这些方法不理解区域。它固定在3.0。 – 2011-05-06 16:26:46