2011-03-25 114 views
44

我已經爲我的MVC3項目添加了一個新的區域,並且我試圖從_Layout頁面鏈接到新區域。我添加了一個名爲'Admin'的區域,其中有一個控制器'Meets'。「區域」之間的ASP.NET MVC`Html.ActionLink`

我使用了視覺工作室設計器來添加區域,使其具有正確的區域註冊類等,並且global.asax文件正在註冊所有區域。

然而,當我用以下2個動作鏈接在根目錄中的頁面,我碰到了一些問題:

@Html.ActionLink("Admin", "Index", "Meets", new { area = "Admin" }, null) 
@Html.ActionLink("Admin", "Index", "Meets", new { area = "" }, null) 

當點擊這兩個鏈接,我被帶到在管理的會見控制器區域,然後應用程序繼續發出錯誤,指出它找不到索引頁面(即使索引頁面存在於Area子目錄的Views文件夾中)。

第1個鏈接的href看起來像這樣:

http://localhost/BCC/Meets?area=Admin

而對於第二連桿中的href看起來是這樣的:

http://localhost/BCC/Meets

另外,如果我打,我想到要創建的鏈接:

http://localhost/BCC/Admin/Meets

我只是得到一個資源無法找到錯誤。所有非常複雜!我希望有人可以幫助...

+0

在這種情況下,「BCC」是什麼?它是一個虛擬目錄嗎? – 2011-03-25 12:19:16

回答

7

我想通了這一點 - 我創建了一個新的測試項目,並完全沒有同樣的事情,我以前做的,它的工作......然後在進一步檢查兩個項目之間的路線相關的所有事情後,我發現一個差異。

在我的BCC應用Global.asax文件,有代碼的流氓線,曾莫名其妙地出現了:

 public static void RegisterRoutes(RouteCollection routes) 
     { 
      // Problem here 
      routes.Clear(); 

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

      routes.MapRoute(
       "Default", // Route name 
       "{controller}/{action}/{id}", // URL with parameters 
       new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults 
      ); 
     } 

     protected void Application_Start() 
     { 
      AreaRegistration.RegisterAllAreas(); 

      RegisterGlobalFilters(GlobalFilters.Filters); 
      RegisterRoutes(RouteTable.Routes); 
     } 

正如你可以看到我的意見是,在一段時間或其他我在RegisterRoutes的開頭放置了routes.Clear()調用,這意味着在我註冊了Application_Start中的區域之後,我立即清除了我剛纔註冊的內容。

感謝您的幫助......它最終導致了我的救贖!

68

確實是奇怪。這工作完全正常,我的步驟:

  1. 創建使用默認的Visual Studio模板
  2. 添加一個新的ASP.NET MVC 3應用面積在項目中使用Visual Studio設計通過右鍵點擊名爲Admin
  3. ~/Areas/Admin/Controllers/MeetsController

    添加新的控制器:

    public class MeetsController : Controller 
    { 
        public ActionResult Index() 
        { 
         return View(); 
        } 
    } 
    
  4. 添加相應的視圖~/Areas/Admin/Views/Meets/Index.cshtml

  5. 在佈局(~/Views/Shared/_Layout.cshtml)添加鏈接:

    @Html.ActionLink("Admin", "Index", "Meets", new { area = "Admin" }, null) 
    @Html.ActionLink("Admin", "Index", "Meets", new { area = "" }, null) 
    
  6. 運行應用程序。

渲染HTML的錨:

<a href="/Admin/Meets">Admin</a> 
<a href="/Meets">Admin</a> 

正如所預期的第一個鏈接的作品,而第二個則沒有。

那麼你的設置有什麼不同?

+0

區域「」沒有「Admin」控制器,在您的情況下,您使用「MeetsController」創建了一個新的「Admin」區域,因此第一個鏈接應按預期工作。如果您確實想要「管理員」區域的控制器執行「任意位置」,則可以嘗試將所有視圖,部分等移出「管理」區域並移至Web應用程序的根目錄。我不確定這將如何工作。 – 2012-01-21 01:27:39

3

驗證您的AdminAreaRegistration類看起來是這樣的:

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 { action = "Index", id = UrlParameter.Optional } 
     ); 
    } 
} 

和你有這樣的Global.asax.cs

protected void Application_Start() 
{ 
    ... // ViewEngine Registration 
    AreaRegistration.RegisterAllAreas(); 
    ... // Other route registration 
} 
2

我通過執行以下操作來解決此問題。

在我的Global.asax.cs,我有

public static void RegisterGlobalFilters(GlobalFilterCollection filters) 
    { 
     filters.Add(new HandleErrorAttribute()); 
    } 

    public static void RegisterRoutes(RouteCollection routes) 
    { 
     routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 
     routes.IgnoreRoute("{*favicon}", new { favicon = @"(.*/)?favicon.ico(/.*)?" }); 
    } 

    protected void Application_Start() 
    { 
     //Initialise IoC 
     IoC.Initialise(); 

     AreaRegistration.RegisterAllAreas(); 
     RegisterGlobalFilters(GlobalFilters.Filters); 
     RegisterRoutes(RouteTable.Routes); 
    } 

在我PublicAreaRegistration.cs(公共區域),我有

public class PublicAreaRegistration : AreaRegistration 
{ 
    public override string AreaName 
    { 
     get 
     { 
      return "Public"; 
     } 
    } 

    public override void RegisterArea(AreaRegistrationContext context) 
    { 
     context.MapRoute("Root", "", new { controller = "Home", action = "Index" }); 

     context.MapRoute(
      "Public_default", 
      "Public/{controller}/{action}/{id}", 
      new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
      , new[] { "<Project Namespace here>.Areas.Public.Controllers" } 
     ); 
    } 
} 

在我AuthAreaRegistration.cs(區受限訪問),我有

public class AuthAreaRegistration : AreaRegistration 
{ 
    public override string AreaName 
    { 
     get 
     { 
      return "Auth"; 
     } 
    } 

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

最後,我在我的* .cshtml網頁的鏈接會像

1)@ Html.ActionLink( 「註銷」, 「註銷」,新的{面積= 「公開」,控制器= 「主頁」})

2)@ Html.ActionLink (「Admin Area」,「Index」,new {area =「Auth」,controller =「Home」})

希望這可以節省一些小時的研究!順便說一句,我在這裏談論MVC3。

Kwex。

+0

給讀者的一個說明,這裏的ActionLink例子會導致OP遇到同樣的問題,儘管有其他變化,他們應該寫成如下: 1)@ Html.ActionLink(「註銷」,「註銷」 ,new {area =「Public」,controller =「Home」},null) 或 2)@ Html.ActionLink(「Admin Area」,「Index」,new {area =「Auth」,controller =「首頁「},null) – 2012-01-21 01:21:07

28

另一種選擇是利用RouteLink()而不是ActionLink的(),它繞過共區域的註冊:

ActionLink的版本:

Html.ActionLink("Log Off", "LogOff", "Account", new { area = "" }, null) 

RouteLink版本:

Html.RouteLink("Log Off", "Default", 
    new { action = "LogOff", controller = "Account" }) 

的第二個參數是在「Global.asax.cs」和各種'AreaRegistration'子類中註冊的「路由名稱」。要使用「RouteLink」鏈接不同區域,只需指定正確的路由名稱。

這下面的例子顯示瞭如何將生成一個共享的部分,不管我「在」的面積可以正常工作三通到不同的區域(如果有的話):

@Html.RouteLink("Blog", "Blog_default", 
    new { action = "Index", controller = "Article" }) 
<br/> 
@Html.RouteLink("Downloads", "Download_default", 
    new { action = "Index", controller = "Download" }) 
<br/> 
@Html.RouteLink("About", "Default", 
    new { action = "Index", controller = "About" }) 

編碼愉快!

1

對於大多數開發人員來說,情況可能並非如此,但是當我添加了我的第一個區域並且未構建我的解決方案時,我遇到了此問題。只要我構建我的解決方案,鏈接就開始正確填充。