日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > windows >内容正文

windows

Mvc系统学习9——Areas学习

發布時間:2023/12/20 windows 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Mvc系统学习9——Areas学习 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

? 在Mvc2.0中,新增加了一個特性就是Areas。在沒有有使用Areas的情況下,我們的Mvc項目組織是下面這樣的。當項目龐大的時候,Controllers,Model,View文件下下面勢必會有很多文件。項目將難以管理。

????? 通過使用Areas使我們可以很好的組織項目,通過單機添加Areas(區域),使用Areas來組織項目??梢缘玫叫碌捻椖拷M織結構。

????? First,Second對應著我們項目的子模塊(First,Second命名不是很好)。在這兩個文件夾下,有各自獨立的Controllers,Models,Views。此外還多了個文件AreaRegistration為后綴的.cs文件. 這個文件主要的作用是給Areas下的子模塊配置路由。在全局文件Global.asax中的Application_Start事件里有這么一句代碼?AreaRegistration.RegisterAllAreas(),通過mvc源碼可以發現通過這句代碼最后會調用各個AreaRegistration類,實現路由的注冊。

//重寫了AreaName用于給DataToken["area"]賦值public class FirstAreaRegistration : AreaRegistration{public override string AreaName{get{return "First";}}//這里會添加一個相應的Area名稱的前綴,因為下面這樣的添加也是作用到全局路由表的.//而且這里的MapRoutes,和在全局的MapRoutes是不同的//這時AreaRegistrationContext里面的方法,它里面的方法會自動給DataToken["area"]鍵賦值當前的AreaName public override void RegisterArea(AreaRegistrationContext context){context.MapRoute("First_default","First/{controller}/{action}/{id}",new { controller = "Home", action = "Index", id = UrlParameter.Optional });} View Code

???????當調用?AreaRegistration.RegisterAllAreas()時,會最后調用下面這個方法。通過下面的代碼可以得出,這個方法會通過反射得出所有的AreaRegistration的Type實例。接下來依次通過Activator創建實例,進而調用CreateContentAndRegister方法。\

internal static void RegisterAllAreas(RouteCollection routes, IBuildManager buildManager, object state) {List<Type> areaRegistrationTypes = TypeCacheUtil.GetFilteredTypesFromAssemblies(_typeCacheName, IsAreaRegistrationType, buildManager);foreach (Type areaRegistrationType in areaRegistrationTypes) {AreaRegistration registration = (AreaRegistration)Activator.CreateInstance(areaRegistrationType);registration.CreateContextAndRegister(routes, state);}} View Code

? ? ? ?registration.CreateContextAndRegister方法:

internal void CreateContextAndRegister(RouteCollection routes, object state) {//實例化一個AreaRegistrationContext 實例,AreaName已經傳遞給AreaRegistrationContext了AreaRegistrationContext context = new AreaRegistrationContext(AreaName, routes, state);string thisNamespace = GetType().Namespace;if (thisNamespace != null) {context.Namespaces.Add(thisNamespace + ".*");}//調用注冊方法,這個方法在各個AreaRegistration中被重寫 RegisterArea(context);} View Code

? ? ? 各個AreaRegistration重寫的RegisterArea(context)方法里面通過調用AreaRegistrationContext的Maproute方法來實現注冊,這個方法最后的調用方法是下面這個??梢园l現下面這個方法也會調用RouteCollection的MapRoute方法,調用這個方法之后,又向route的DataTokens字典的area和UseNamespaceFallback鍵設置值。這有什么作用?

public Route MapRoute(string name, string url, object defaults, object constraints, string[] namespaces) {if (namespaces == null && Namespaces != null) {namespaces = Namespaces.ToArray();}Route route = Routes.MapRoute(name, url, defaults, constraints, namespaces);route.DataTokens["area"] = AreaName;// disabling the namespace lookup fallback mechanism keeps this areas from accidentally picking up// controllers belonging to other areasbool useNamespaceFallback = (namespaces == null || namespaces.Length == 0);route.DataTokens["UseNamespaceFallback"] = useNamespaceFallback;return route;} View Code

? ? ? ? 還有幾個疑問待解決:

??????? 1.Area下的路由過程是怎么樣的?
??????? 2.在Area下可以實現不同的Area有相同的ControllerName,可以在全局的Global.ascx進行文件配置,但是如果是同名的Controller則會出現錯誤,而對沒有同名的Controller進行配置則不會,這是為什么?

????????比如在First,Second都有HomeController,如果在全局的Global.ascx文件進行配置,下面的配置不會出錯,但是如果namespaces是MvcAreaDemo2.Areas.First時則會出現錯誤。這時為什么?如果沒有用同名的Controller則namespace是MvcAreaDemo2.Areas.First和MvcAreaDemo2.Areas.First.Controllers都沒有錯。

routes.Add(new Route("Index1", new MvcRouteHandler()){Defaults = new RouteValueDictionary(new { controller = "Home", action = "Index", id = UrlParameter.Optional }),DataTokens = new RouteValueDictionary(new { area = "First", namespaces = new[] { "MvcAreaDemo2.Areas.First.Controllers" } })});routes.Add(new Route("Index2", new MvcRouteHandler()){Defaults = new RouteValueDictionary(new { controller = "Home", action = "Index", id = UrlParameter.Optional }),DataTokens = new RouteValueDictionary(new { area = "Second", namespaces = new[] { "MvcAreaDemo2.Areas.Second.Controllers" } })}); View Code

? ? ? ? 3.在Area的使用況下,如果要跳轉到另外一個Area的頁面,則使用Html.ActionLink進行編寫似乎更加麻煩了,有木有更加簡便的方法?

轉載于:https://www.cnblogs.com/fhlj/p/3615244.html

總結

以上是生活随笔為你收集整理的Mvc系统学习9——Areas学习的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。