自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

給ASP.NET MVC及WebApi添加路由優(yōu)先級

開發(fā) 后端
這是一個對Asp.Net Mvc的一個很小的功能拓展,小項目可能不太需要這個功能,但有時候項目大了注冊的路由不生效時你應該要想到有可能是因為路由順序的原因,這時這個路由優(yōu)先級的功能有可能就會給你帶來便利。

一、為什么需要路由優(yōu)先級

大家都知道我們在Asp.Net MVC項目或WebApi項目中注冊路由是沒有優(yōu)先級的,當項目比較大、或有多個區(qū)域、或多個Web項目、或采用插件式框架開發(fā)時,我們的路由注冊很可能 不是寫在一個文件中的,而是分散在很多不同項目的文件中,這樣一來,路由的優(yōu)先級的問題就突顯出來了。

比如: App_Start/RouteConfig.cs中

 

  1. routes.MapRoute( 
  2.     name: "Default"
  3.     url: "{controller}/{action}/{id}"
  4.     defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
  5. ); 
  6.  
  7. Areas/Admin/AdminAreaRegistration.cs中 
  8.  
  9. context.MapRoute( 
  10.     name: "Login",    
  11.     url: "login"
  12.     defaults: new { area = "Admin", controller = "Account", action = "Login", id = UrlParameter.Optional }, 
  13.     namespaces: new string[] { "Wenku.Admin.Controllers" } 
  14. ); 

假如是先注冊上面那個通用的default路由,再注冊這個login的路由,那么無論怎么樣,都會先匹配***個滿足條件的路由,也就是第兩個路由注冊是無效的。
造成這個問題的原因就是這兩個路由注冊的順序問題,而Asp.Net MVC及WebApi中注冊路由都沒有優(yōu)先級這個概念,所以今天我們就是要自己實現(xiàn)這個想法,在注冊路由時加入一個優(yōu)先級的概念。

二、解決思路

1、先分析路由注冊的入口,比如我們新建一個mvc4.0的項目

 
  1. public class MvcApplication : System.Web.HttpApplication 
  2.     protected void Application_Start() 
  3.     { 
  4.         AreaRegistration.RegisterAllAreas(); 
  5.  
  6.         WebApiConfig.Register(GlobalConfiguration.Configuration); 
  7.         FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); 
  8.         RouteConfig.RegisterRoutes(RouteTable.Routes); 
  9.     } 

Mvc路由的注冊入口有兩個:
a. AreaRegistration.RegisterAllAreas();                            注冊區(qū)域路由
b. RouteConfig.RegisterRoutes(RouteTable.Routes);          注冊項目路由

WebApi路由注冊入口有一個:
WebApiConfig.Register(GlobalConfiguration.Configuration);  注冊WebApi路由

2、注冊路由的處理類分析

AreaRegistrationContext
RouteCollection
HttpRouteCollection

注冊路由時主要是由這三個類來注冊處理路由的。

3、路由優(yōu)先級方案

a、更改路由的注冊入口
b、自定義一個路由的結(jié)構(gòu)類RoutePriority及HttpRoutePriority,這兩個類下面都有Priority這個屬性
c、自定一個RegistrationContext來注冊路由,注冊的對象為上述自定義路由。
d、所有的路由注冊完成之后再按優(yōu)先順序添加到RouteCollection及HttpRouteCollection中實際生效。

#p#

三、具體實現(xiàn)

1、路由定義

  1. public class RoutePriority : Route 
  2.     public string Name { get; set; } 
  3.     public int Priority { get; set; } 
  4.  
  5.     public RoutePriority(string url, IRouteHandler routeHandler) 
  6.         : base(url,routeHandler) 
  7.     { 
  8.  
  9.     } 
  10.  
  11. public class HttpRoutePriority 
  12.     public string Name { get; set; } 
  13.     public int Priority { get; set; } 
  14.     public string RouteTemplate{get;set;} 
  15.     public object Defaults{get;set;} 
  16.     public object Constraints{get;set;} 
  17.     public HttpMessageHandler Handler{get;set;} 

2、定義路由注冊的接口

  1. public interface IRouteRegister 
  2.     void Register(RegistrationContext context); 

3、定義路由注冊上下文類

  1. public class RegistrationContext 
  2.     #region mvc 
  3.     public List<RoutePriority> Routes = new List<RoutePriority>(); 
  4.  
  5.     public RoutePriority MapRoute(string name, string url,int priority=0
  6.     { 
  7.         return MapRoute(name, url, (object)null /* defaults */, priority); 
  8.     } 
  9.  
  10.     public RoutePriority MapRoute(string name, string url, object defaults, int priority = 0
  11.     { 
  12.         return MapRoute(name, url, defaults, (object)null /* constraints */, priority); 
  13.     } 
  14.  
  15.     public RoutePriority MapRoute(string name, string url, object defaults, object constraints, int priority = 0
  16.     { 
  17.         return MapRoute(name, url, defaults, constraints, null /* namespaces */, priority); 
  18.     } 
  19.  
  20.     public RoutePriority MapRoute(string name, string url, string[] namespaces, int priority = 0
  21.     { 
  22.         return MapRoute(name, url, (object)null /* defaults */, namespaces, priority); 
  23.     } 
  24.  
  25.     public RoutePriority MapRoute(string name, string url, object defaults, string[] namespaces,int priority=0
  26.     { 
  27.         return MapRoute(name, url, defaults, null /* constraints */, namespaces, priority); 
  28.     } 
  29.  
  30.     public RoutePriority MapRoute(string name, string url, object defaults, object constraints, string[] namespaces, int priority = 0
  31.     { 
  32.         var route = MapPriorityRoute(name, url, defaults, constraints, namespaces, priority); 
  33.         var areaName = GetAreaName(defaults); 
  34.         route.DataTokens["area"] = areaName; 
  35.  
  36.         // disabling the namespace lookup fallback mechanism keeps this areas from accidentally picking up 
  37.         // controllers belonging to other areas 
  38.         bool useNamespaceFallback = (namespaces == null || namespaces.Length == 0); 
  39.         route.DataTokens["UseNamespaceFallback"] = useNamespaceFallback; 
  40.  
  41.         return route; 
  42.     } 
  43.  
  44.     private static string GetAreaName(object defaults) 
  45.     { 
  46.         if (defaults != null
  47.         { 
  48.             var property = defaults.GetType().GetProperty("area"); 
  49.             if (property != null
  50.                 return (string)property.GetValue(defaults, null); 
  51.         } 
  52.  
  53.         return null
  54.     } 
  55.  
  56.     private RoutePriority MapPriorityRoute(string name, string url, object defaults, object constraints, string[] namespaces,int priority) 
  57.     { 
  58.         if (url == null
  59.         { 
  60.             throw new ArgumentNullException("url"); 
  61.         } 
  62.  
  63.         var route = new RoutePriority(url, new MvcRouteHandler()) 
  64.         { 
  65.             Name = name, 
  66.             Priority = priority, 
  67.             Defaults = CreateRouteValueDictionary(defaults), 
  68.             Constraints = CreateRouteValueDictionary(constraints), 
  69.             DataTokens = new RouteValueDictionary() 
  70.         }; 
  71.  
  72.         if ((namespaces != null) && (namespaces.Length > 0)) 
  73.         { 
  74.             route.DataTokens["Namespaces"] = namespaces; 
  75.         } 
  76.  
  77.         Routes.Add(route); 
  78.         return route; 
  79.     } 
  80.  
  81.     private static RouteValueDictionary CreateRouteValueDictionary(object values) 
  82.     { 
  83.         var dictionary = values as IDictionary<string, object>; 
  84.         if (dictionary != null
  85.         { 
  86.             return new RouteValueDictionary(dictionary); 
  87.         } 
  88.  
  89.         return new RouteValueDictionary(values); 
  90.     } 
  91.     #endregion 
  92.  
  93.     #region http 
  94.     public List<HttpRoutePriority> HttpRoutes = new List<HttpRoutePriority>(); 
  95.  
  96.     public HttpRoutePriority MapHttpRoute(string name, string routeTemplate, int priority = 0
  97.     { 
  98.         return MapHttpRoute(name, routeTemplate, defaults: null, constraints: null, handler: null, priority: priority); 
  99.     } 
  100.  
  101.     public HttpRoutePriority MapHttpRoute(string name, string routeTemplate, object defaults, int priority = 0
  102.     { 
  103.         return MapHttpRoute(name, routeTemplate, defaults, constraints: null, handler: null, priority: priority); 
  104.     } 
  105.  
  106.     public HttpRoutePriority MapHttpRoute(string name, string routeTemplate, object defaults, object constraints, int priority = 0
  107.     { 
  108.         return MapHttpRoute(name, routeTemplate, defaults, constraints, handler: null, priority: priority); 
  109.     } 
  110.  
  111.     public HttpRoutePriority MapHttpRoute(string name, string routeTemplate, object defaults, object constraints, HttpMessageHandler handler, int priority = 0
  112.     { 
  113.         var httpRoute = new HttpRoutePriority(); 
  114.         httpRoute.Name = name; 
  115.         httpRoute.RouteTemplate = routeTemplate; 
  116.         httpRoute.Defaults = defaults; 
  117.         httpRoute.Constraints = constraints; 
  118.         httpRoute.Handler = handler; 
  119.         httpRoute.Priority = priority; 
  120.         HttpRoutes.Add(httpRoute); 
  121.  
  122.         return httpRoute; 
  123.     } 
  124.     #endregion 

4、把路由注冊處理方法添加到Configuration類中

  1. public static Configuration RegisterRoutePriority(this Configuration config) 
  2.     var typesSoFar = new List<Type>(); 
  3.     var assemblies = GetReferencedAssemblies(); 
  4.     foreach (Assembly assembly in assemblies) 
  5.     { 
  6.         var types = assembly.GetTypes().Where(t => typeof(IRouteRegister).IsAssignableFrom(t) && !t.IsAbstract && !t.IsInterface); 
  7.         typesSoFar.AddRange(types); 
  8.     } 
  9.  
  10.     var context = new RegistrationContext(); 
  11.     foreach (var type in typesSoFar) 
  12.     { 
  13.         var obj = (IRouteRegister)Activator.CreateInstance(type); 
  14.         obj.Register(context); 
  15.     } 
  16.  
  17.     foreach (var route in context.HttpRoutes.OrderByDescending(x => x.Priority)) 
  18.         GlobalConfiguration.Configuration.Routes.MapHttpRoute(route.Name, route.RouteTemplate, route.Defaults, route.Constraints, route.Handler); 
  19.  
  20.     foreach (var route in context.Routes.OrderByDescending(x => x.Priority)) 
  21.         RouteTable.Routes.Add(route.Name, route); 
  22.  
  23.     return config; 
  24.  
  25. private static IEnumerable<Assembly> GetReferencedAssemblies() 
  26.     var assemblies = BuildManager.GetReferencedAssemblies(); 
  27.     foreach (Assembly assembly in assemblies) 
  28.         yield return assembly; 

這樣一來就大功告成,使用時只需要在Global.asax.cs文件中修改原注冊入口為

  1. public class MvcApplication : System.Web.HttpApplication 
  2.     protected void Application_Start() 
  3.     { 
  4.         WebApiConfig.Register(GlobalConfiguration.Configuration); 
  5.         FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); 
  6.         RouteConfig.RegisterRoutes(RouteTable.Routes); 
  7.  
  8.         Configuration.Instance() 
  9.             .RegisterComponents() 
  10.             .RegisterRoutePriority(); //注冊自定義路由 
  11.     } 

在每個項目中使用只需要要繼承自定義路由注冊接口IRouteRegister,例如:

  1. public class Registration : IRouteRegister 
  2.     public void Register(RegistrationContext context) 
  3.     { 
  4.        //注冊后端管理登錄路由 
  5.         context.MapRoute( 
  6.           name: "Admin_Login"
  7.           url: "Admin/login"
  8.           defaults: new { area = "Admin", controller = "Account", action = "Login", id = UrlParameter.Optional }, 
  9.           namespaces: new string[] { "Wenku.Admin.Controllers" }, 
  10.           priority: 11 
  11.       ); 
  12.  
  13.        //注冊后端管理頁面默認路由 
  14.         context.MapRoute( 
  15.             name: "Admin_default"
  16.             url: "Admin/{controller}/{action}/{id}"
  17.             defaults: new { area = "Admin", controller = "Home", action = "Index", id = UrlParameter.Optional }, 
  18.             namespaces: new string[] { "Wenku.Admin.Controllers" }, 
  19.             priority: 10 
  20.         ); 
  21.  
  22.        //注冊手機訪問WebApi路由 
  23.         context.MapHttpRoute( 
  24.             name: "Mobile_Api"
  25.             routeTemplate: "api/mobile/{controller}/{action}/{id}"
  26.             defaults: new 
  27.             { 
  28.                 area = "mobile"
  29.                 action = RouteParameter.Optional, 
  30.                 id = RouteParameter.Optional, 
  31.                 namespaceName = new string[] { "Wenku.Mobile.Http" } 
  32.             }, 
  33.             constraints: new { action = new StartWithConstraint() }, 
  34.             priority: 0 
  35.         ); 
  36.     } 

四、總結(jié)

這是一個對Asp.Net Mvc的一個很小的功能拓展,小項目可能不太需要這個功能,但有時候項目大了注冊的路由不生效時你應該要想到有可能是因為路由順序的原因,這時這個路由優(yōu)先級的功能有可能就會給你帶來便利??傊蚕斫o有需要的朋友們參考。

責任編輯:王雪燕 來源: 蕭秦的博客
相關推薦

2015-10-29 13:15:50

ASP.NETWebApi路由

2009-07-22 15:27:39

ASP.NET MVC自定義路由

2010-06-25 08:51:46

ASP.NET MVC

2009-07-24 13:08:52

DataRowASP.NET 2.0

2009-07-24 13:20:44

MVC框架ASP.NET

2009-07-31 12:43:59

ASP.NET MVC

2014-08-21 09:31:06

路由

2015-01-07 09:32:50

ASP.NET MVC路由

2009-07-22 15:02:02

ASP.NET MVC

2009-07-23 15:44:39

ASP.NET MVC

2009-07-22 10:09:59

ASP.NET MVC

2009-07-22 13:24:24

ASP.NET MVC

2009-07-23 14:31:20

ASP.NET MVC

2009-07-20 10:53:59

ASP.NET MVC

2009-11-02 09:14:51

ASP.NET MVC

2010-03-12 09:38:58

2025-01-10 00:41:38

版本控制API

2010-06-23 15:44:03

ASP.NET MVC

2009-07-24 11:33:12

MVC單元測試ASP.NET

2009-07-22 10:34:37

ActionInvokASP.NET MVC
點贊
收藏

51CTO技術(shù)棧公眾號