標籤:style blog http color strong 檔案
Areas是ASP.NET Mvc 2.0版本中引入的眾多新特性之一,它可以幫你把一個較大型的Web項目分成若干組成部分,即Area。實現Area的功能可以有兩個組織形式:
- 在1個ASP.NET Mvc 2.0 Project中建立Areas。
- 建立多個ASP.NET Mvc 2.0 Project,每個Project就是一個Area。
第2種結構比較複雜,但第1種結構同樣可以做到每個Area之間的並行開發,互不影響,所以不推薦第2種方法。
以ASP.NET Mvc 2.0的預設範本為例:
1. 首先要建立一個ASP.NET Mvc 2.0的Project,在Project上點擊滑鼠右鍵選擇Add->Area,在開啟的視窗中輸入Area的名子(例如:Profile),點擊Add按鈕,然後將看到下面的結構。
名子叫做Profile的Area的結構與根目錄下的Controllers,Models和Views的結構是一樣的,唯一區別是Profile下面多了一個ProfileAreaRegistration.cs檔案。它繼承自AreaRegistration類,ProfileAreaRegistration 必須實現AreaRegistration類中的AreaName屬性和RegisterArea(AreaRegistrationContext context)方法
using System.Web.Mvc; namespace ASP.NET_Mvc_2_Test.Areas.Profile { public class ProfileAreaRegistration : AreaRegistration { public override string AreaName { get{return "Profile";} } public override void RegisterArea(AreaRegistrationContext context) { context.MapRoute( "Profile_default", "Profile/{controller}/{action}/{id}", new { action = "Index", id = UrlParameter.Optional}); } } }
AreaName屬性用來定義Area的名子, RegisterArea(AreaRegistrationContext context) 方法中可以看出在瀏覽器的地址欄中URL的樣式為Profile/{controller}/{action}/{id},是4級構結,只要將context.MapRoute(…)改為
public override void RegisterArea(AreaRegistrationContext context) { context.MapRoute( "Profile_default", "Profile/{action}/{id}", new { controller = "要訪問的controller名子", action = "Index", id = UrlParameter.Optional }); }
URL的樣式會再變為三級結構 Profile/{action}/{id}。
2. 修改根目錄下Views/shared/Site.Master檔案,並添加一個名為o”Your Profile”的功能表項目並指定area的名子, 樣本中Area的名子為Profile。
<ul id="menu"> <li><%= Html.ActionLink("Home", "Index", "Home", new { area = ""}, null)%></li> <li><%= Html.ActionLink("Your Profile", "ViewProfile", "Profile", new { area = "Profile" }, null)%></li> <li><%= Html.ActionLink("About", "About", "Home", new { area = ""}, null)%></li> </ul>
注意:Home和About不屬於任何Area,但是也要通過匿名對象的方式聲明area,如果沒有聲明Area,當進入到 Profile的某個view時, Home和About的會的URL會變為Profile/Home, Profile/About,點擊Home或About時會有異常拋出,所以當頁面上的某個連結不屬於任何一個Area並且有可能被多個Area可享的 話,一定要加上new { area = ""}
3. 只有匹配正確的Route才能顯示Area中的View,Area中的Route已經配置好,但它是如何被加入到RouteTable中的?
項目分為三個首頁
如: /Home/Index 前台首頁
/Admin/Home/Index 後台首頁
/OA/Home/Index 辦公平台首頁
建立一個asp.net MVC3 樣本項目: 右鍵 →添加→Area
直接運行項目:
原因是存在同名的多個Controller,需要配置預設的命名空間。解決方案:
開啟Global.asax.cs
public static void RegisterRoutes(RouteCollection routes) { 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 new[] { "Web.Controllers" }// Namespaces 引入預設的命名空間 );
}
http://localhost:49849/ 運行後輸出 Home/Index
http://localhost:49849/Admin/Home/Index 運行後輸出 Admin/Home/Index
http://localhost:49849/OA/Home/Index 運行後輸出 OA/Home/Index
更改路徑:
http://localhost:49849/Admin/ www.2cto.com後報404錯誤
原因是 Area下面的Admin沒有配置預設的 Controller造成的,解決方案:
開啟 Area下Admin下 AdminAreaRegistration.cs
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
加上預設的Controller即可。