国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

MVC4中使用Area和注意的地方

2019-11-14 13:50:01
字體:
來源:轉載
供稿:網友

在MVC項目中經常會使用到Area來分開不同的模塊讓項目結構更加的清晰。

步驟如下:

 項目 –> 添加 -> 區域 (Area)

 輸入 Admin

添加成功后

Area包含:
創建一個空MVC工程結構類似,Admin Area 有自己的 Controllers、Models 和 Views 文件夾,不一樣的地方就是多了一個 AdminAreaRegistration.cs 文件,這個文件中定義了一個叫 AdminAreaRegistration 的類,它的內容如下:

 

根目錄可以放一套一樣的結構用來做前端開發使用,而admin 目錄一般會作為管理員后臺來開發!

 

AdminAreaRegistration.cs 文件,這個文件中定義了一個叫 AdminAreaRegistration 的類,它的內容如下:

 1 namespace MvcApp4.Areas.Admin 2 { 3     public class AdminAreaRegistration : AreaRegistration 4     { 5         public override string AreaName 6         { 7             get 8             { 9                 return "Admin";10             }11         }12 13         public override void RegisterArea(AreaRegistrationContext context)14         {15             context.MaPRoute(16                 "Admin_default",17                 "Admin/{controller}/{action}/{id}",18                 new { controller = "home", action = "Index", id = UrlParameter.Optional },19                 namespaces: new[] { "MvcApp4.Areas.Admin.Controllers" } //指定該路由查找控制器類的命名空間20             );21         }22     }23 }

 

 在這里需要注意需加入 Areas 所在的命名空間,來控制 controllers 接收的參數,不然訪問會出現錯誤,往下一點會提到。

namespaces: new[] { "MvcApp4.Areas.Admin.Controllers" }

AreaRegistrationContext 類的 MapRoute 方法和 App_Start-> RouteConfig.cs  的 MapRoute 方法的使用是一樣的,只是區分Area 目錄下的路由控制!

在 Global.asax 中的 application_Start 方法會自動加了這樣一句代碼

1 protected void Application_Start() {2     AreaRegistration.RegisterAllAreas();3 4     WebApiConfig.Register(GlobalConfiguration.Configuration);5     FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);6     RouteConfig.RegisterRoutes(RouteTable.Routes);7     BundleConfig.RegisterBundles(BundleTable.Bundles);8 }

 

調用 AreaRegistration.RegisterAllAreas 方法讓MVC應用程序在啟動后會尋找所有繼承自 AreaRegistration 的類,并為每個這樣的類調用它們的 RegisterArea 方法。

 

下面我們來做一個 Demo

 新建兩個訪問連接,內容都是一樣,都是簡單輸出一個 "hello World"

URL定位到 (areas/admin)

http://localhost:18291/Admin/Home/Index

URL定位到(根目錄)

http://localhost:18291/Home/Index 

 

 1     public class HomeController : Controller 2     { 3         // 4         // GET: /Admin/Home/ 5  6         public ActionResult Index() 7         { 8             return Content("hello world"); 9         }10 11     }

 

 

 

 如果剛才沒有加入:

namespaces: new[] { "MvcApp4.Areas.Admin.Controllers" }

運行后就會出現如下錯誤:

 

但是如果我們把根目錄下的  /Home/Index  的內容輸出改成  “Root Say hello World” , 你會發現還是輸出 “hello World”,

這是就是  Controller的歧義問題

這就是我們需要注意的另一個地方

我們需要在App_start下的 RouteConfig.cs 也要增加一個 namespaces 來聲明 Controller 訪問的命名空間!

 

   //App_start下的 RouteConfig.cs   
public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }, namespaces: new[] { "MvcApp4.Controllers" }//指定該路由查找控制器類的命名空間 controllers ); } } //areas 下的 /Admin/AdminAreaRegistration.cs 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 = "home", action = "Index", id = UrlParameter.Optional }, namespaces: new[] { "MvcApp4.Areas.Admin.Controllers" } //對應的命名空間的 controllers ); } }

 

 

這樣訪問時就可以區分 , 不同目錄的 controller  

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 新疆| 昂仁县| 阿城市| 偏关县| 黎平县| 河池市| 信宜市| 绥滨县| 丰城市| 衡山县| 自治县| 屏南县| 方城县| 汤阴县| 晋城| 阳高县| 赤城县| 湘乡市| 白朗县| 汝南县| 辽源市| 大港区| 石屏县| 永和县| 江永县| 旬邑县| 澄江县| 石景山区| 渭源县| 依安县| 平邑县| 泗阳县| 泰安市| 石景山区| 昆明市| 枞阳县| 思南县| 宁城县| 舞钢市| 霍邱县| 建平县|