2011-04-17 99 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