Mvc系统学习9——Areas学习
? 在Mvc2.0中,新增加了一個特性就是Areas。在沒有有使用Areas的情況下,我們的Mvc項目組織是下面這樣的。當項目龐大的時候,Controllers,Model,View文件下下面勢必會有很多文件。項目將難以管理。
????? 通過使用Areas使我們可以很好的組織項目,通過單機添加Areas(區域),使用Areas來組織項目。可以得到新的項目組織結構。
????? 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方法來實現注冊,這個方法最后的調用方法是下面這個。可以發現下面這個方法也會調用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学习的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: MarkDown2安装后不能预览的问题
 - 下一篇: 专利CPC客户端保存文件出现Schema