MVC2 Area實現網站多級目錄

來源:互聯網
上載者:User

標籤:style   blog   http   color   strong   檔案   

Areas是ASP.NET Mvc 2.0版本中引入的眾多新特性之一,它可以幫你把一個較大型的Web項目分成若干組成部分,即Area。實現Area的功能可以有兩個組織形式:

  1. 在1個ASP.NET Mvc 2.0 Project中建立Areas。
  2. 建立多個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即可。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.