为ASP.NET MVC及WebApi添加路由优先级

冉冉升起的如银的炊烟,那更古沉默永不停息的小溪,那驮着夕阳缓缓独行的老牛,一方方秧田像棋盘格子,绿绒绒的秧苗,织成一幅幅地毯,远远地伸向天际;丛丛绿树掩映着青砖红瓦的房屋。春天,故乡的松树林依然郁郁苍苍,映山红却已开遍山丘。

一、为什么需要路由优先级

大家都知道我们在Asp.Net MVC项目或WebApi项目中注册路由是没有优先级的,当项目比较大、或有多个区域、或多个Web项目、或采用插件式框架开发时,我们的路由注册很可能 不是写在一个文件中的,而是分散在很多不同项目的文件中,这样一来,路由的优先级的问题就突显出来了。

比如: App_Start/RouteConfig.cs中

routes.MapRoute( 
  name: "Default", 
  url: "{controller}/{action}/{id}", 
  defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
); 
 
Areas/Admin/AdminAreaRegistration.cs中 
 
context.MapRoute( 
  name: "Login",  
  url: "login", 
  defaults: new { area = "Admin", controller = "Account", action = "Login", id = UrlParameter.Optional }, 
  namespaces: new string[] { "Wenku.Admin.Controllers" } 
); 

假如是先注册上面那个通用的default路由,再注册这个login的路由,那么无论怎么样,都会先匹配第一个满足条件的路由,也就是第两个路由注册是无效的。
造成这个问题的原因就是这两个路由注册的顺序问题,而Asp.Net MVC及WebApi中注册路由都没有优先级这个概念,所以今天我们就是要自己实现这个想法,在注册路由时加入一个优先级的概念。

二、解决思路

1、先分析路由注册的入口,比如我们新建一个mvc4.0的项目

public class MvcApplication : System.Web.HttpApplication 
{ 
  protected void Application_Start() 
  { 
    AreaRegistration.RegisterAllAreas(); 
 
    WebApiConfig.Register(GlobalConfiguration.Configuration); 
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); 
    RouteConfig.RegisterRoutes(RouteTable.Routes); 
  } 
} 

Mvc路由的注册入口有两个:
a. AreaRegistration.RegisterAllAreas(); 注册区域路由
b. RouteConfig.RegisterRoutes(RouteTable.Routes); 注册项目路由

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

2、注册路由的处理类分析

AreaRegistrationContext
RouteCollection
HttpRouteCollection

注册路由时主要是由这三个类来注册处理路由的。

3、路由优先级方案

a、更改路由的注册入口
b、自定义一个路由的结构类RoutePriority及HttpRoutePriority,这两个类下面都有Priority这个属性
c、自定一个RegistrationContext来注册路由,注册的对象为上述自定义路由。
d、所有的路由注册完成之后再按优先顺序添加到RouteCollection及HttpRouteCollection中实际生效。

三、具体实现

1、路由定义

public class RoutePriority : Route 
{ 
  public string Name { get; set; } 
  public int Priority { get; set; } 
 
  public RoutePriority(string url, IRouteHandler routeHandler) 
    : base(url,routeHandler) 
  { 
 
  } 
} 
 
public class HttpRoutePriority 
{ 
  public string Name { get; set; } 
  public int Priority { get; set; } 
  public string RouteTemplate{get;set;} 
  public object Defaults{get;set;} 
  public object Constraints{get;set;} 
  public HttpMessageHandler Handler{get;set;} 
} 

2、定义路由注册的接口

public interface IRouteRegister 
{ 
  void Register(RegistrationContext context); 
} 

3、定义路由注册上下文类

public class RegistrationContext 
{ 
  #region mvc 
  public List<RoutePriority> Routes = new List<RoutePriority>(); 
 
  public RoutePriority MapRoute(string name, string url,int priority=0) 
  { 
    return MapRoute(name, url, (object)null /* defaults */, priority); 
  } 
 
  public RoutePriority MapRoute(string name, string url, object defaults, int priority = 0) 
  { 
    return MapRoute(name, url, defaults, (object)null /* constraints */, priority); 
  } 
 
  public RoutePriority MapRoute(string name, string url, object defaults, object constraints, int priority = 0) 
  { 
    return MapRoute(name, url, defaults, constraints, null /* namespaces */, priority); 
  } 
 
  public RoutePriority MapRoute(string name, string url, string[] namespaces, int priority = 0) 
  { 
    return MapRoute(name, url, (object)null /* defaults */, namespaces, priority); 
  } 
 
  public RoutePriority MapRoute(string name, string url, object defaults, string[] namespaces,int priority=0) 
  { 
    return MapRoute(name, url, defaults, null /* constraints */, namespaces, priority); 
  } 
 
  public RoutePriority MapRoute(string name, string url, object defaults, object constraints, string[] namespaces, int priority = 0) 
  { 
    var route = MapPriorityRoute(name, url, defaults, constraints, namespaces, priority); 
    var areaName = GetAreaName(defaults); 
    route.DataTokens["area"] = areaName; 
 
    // disabling the namespace lookup fallback mechanism keeps this areas from accidentally picking up 
    // controllers belonging to other areas 
    bool useNamespaceFallback = (namespaces == null || namespaces.Length == 0); 
    route.DataTokens["UseNamespaceFallback"] = useNamespaceFallback; 
 
    return route; 
  } 
 
  private static string GetAreaName(object defaults) 
  { 
    if (defaults != null) 
    { 
      var property = defaults.GetType().GetProperty("area"); 
      if (property != null) 
        return (string)property.GetValue(defaults, null); 
    } 
 
    return null; 
  } 
 
  private RoutePriority MapPriorityRoute(string name, string url, object defaults, object constraints, string[] namespaces,int priority) 
  { 
    if (url == null) 
    { 
      throw new ArgumentNullException("url"); 
    } 
 
    var route = new RoutePriority(url, new MvcRouteHandler()) 
    { 
      Name = name, 
      Priority = priority, 
      Defaults = CreateRouteValueDictionary(defaults), 
      Constraints = CreateRouteValueDictionary(constraints), 
      DataTokens = new RouteValueDictionary() 
    }; 
 
    if ((namespaces != null) && (namespaces.Length > 0)) 
    { 
      route.DataTokens["Namespaces"] = namespaces; 
    } 
 
    Routes.Add(route); 
    return route; 
  } 
 
  private static RouteValueDictionary CreateRouteValueDictionary(object values) 
  { 
    var dictionary = values as IDictionary<string, object>; 
    if (dictionary != null) 
    { 
      return new RouteValueDictionary(dictionary); 
    } 
 
    return new RouteValueDictionary(values); 
  } 
  #endregion 
 
  #region http 
  public List<HttpRoutePriority> HttpRoutes = new List<HttpRoutePriority>(); 
 
  public HttpRoutePriority MapHttpRoute(string name, string routeTemplate, int priority = 0) 
  { 
    return MapHttpRoute(name, routeTemplate, defaults: null, constraints: null, handler: null, priority: priority); 
  } 
 
  public HttpRoutePriority MapHttpRoute(string name, string routeTemplate, object defaults, int priority = 0) 
  { 
    return MapHttpRoute(name, routeTemplate, defaults, constraints: null, handler: null, priority: priority); 
  } 
 
  public HttpRoutePriority MapHttpRoute(string name, string routeTemplate, object defaults, object constraints, int priority = 0) 
  { 
    return MapHttpRoute(name, routeTemplate, defaults, constraints, handler: null, priority: priority); 
  } 
 
  public HttpRoutePriority MapHttpRoute(string name, string routeTemplate, object defaults, object constraints, HttpMessageHandler handler, int priority = 0) 
  { 
    var httpRoute = new HttpRoutePriority(); 
    httpRoute.Name = name; 
    httpRoute.RouteTemplate = routeTemplate; 
    httpRoute.Defaults = defaults; 
    httpRoute.Constraints = constraints; 
    httpRoute.Handler = handler; 
    httpRoute.Priority = priority; 
    HttpRoutes.Add(httpRoute); 
 
    return httpRoute; 
  } 
  #endregion 
} 

4、把路由注册处理方法添加到Configuration类中

public static Configuration RegisterRoutePriority(this Configuration config) 
{ 
  var typesSoFar = new List<Type>(); 
  var assemblies = GetReferencedAssemblies(); 
  foreach (Assembly assembly in assemblies) 
  { 
    var types = assembly.GetTypes().Where(t => typeof(IRouteRegister).IsAssignableFrom(t) && !t.IsAbstract && !t.IsInterface); 
    typesSoFar.AddRange(types); 
  } 
 
  var context = new RegistrationContext(); 
  foreach (var type in typesSoFar) 
  { 
    var obj = (IRouteRegister)Activator.CreateInstance(type); 
    obj.Register(context); 
  } 
 
  foreach (var route in context.HttpRoutes.OrderByDescending(x => x.Priority)) 
    GlobalConfiguration.Configuration.Routes.MapHttpRoute(route.Name, route.RouteTemplate, route.Defaults, route.Constraints, route.Handler); 
 
  foreach (var route in context.Routes.OrderByDescending(x => x.Priority)) 
    RouteTable.Routes.Add(route.Name, route); 
 
  return config; 
} 
 
private static IEnumerable<Assembly> GetReferencedAssemblies() 
{ 
  var assemblies = BuildManager.GetReferencedAssemblies(); 
  foreach (Assembly assembly in assemblies) 
    yield return assembly; 
} 
这样一来就大功告成,使用时只需要在Global.asax.cs文件中修改原注册入口为

public class MvcApplication : System.Web.HttpApplication 
{ 
  protected void Application_Start() 
  { 
    WebApiConfig.Register(GlobalConfiguration.Configuration); 
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); 
    RouteConfig.RegisterRoutes(RouteTable.Routes); 
 
    Configuration.Instance() 
      .RegisterComponents() 
      .RegisterRoutePriority(); //注册自定义路由 
  } 
} 
在每个项目中使用只需要要继承自定义路由注册接口IRouteRegister,例如:

public class Registration : IRouteRegister 
{ 
  public void Register(RegistrationContext context) 
  { 
    //注册后端管理登录路由 
    context.MapRoute( 
     name: "Admin_Login", 
     url: "Admin/login", 
     defaults: new { area = "Admin", controller = "Account", action = "Login", id = UrlParameter.Optional }, 
     namespaces: new string[] { "Wenku.Admin.Controllers" }, 
     priority: 11 
   ); 
 
    //注册后端管理页面默认路由 
    context.MapRoute( 
      name: "Admin_default", 
      url: "Admin/{controller}/{action}/{id}", 
      defaults: new { area = "Admin", controller = "Home", action = "Index", id = UrlParameter.Optional }, 
      namespaces: new string[] { "Wenku.Admin.Controllers" }, 
      priority: 10 
    ); 
 
    //注册手机访问WebApi路由 
    context.MapHttpRoute( 
      name: "Mobile_Api", 
      routeTemplate: "api/mobile/{controller}/{action}/{id}", 
      defaults: new 
      { 
        area = "mobile", 
        action = RouteParameter.Optional, 
        id = RouteParameter.Optional, 
        namespaceName = new string[] { "Wenku.Mobile.Http" } 
      }, 
      constraints: new { action = new StartWithConstraint() }, 
      priority: 0 
    ); 
  } 
} 

四、总结

当遇到大项目注册的路由不生效时你应该要想到有可能是因为路由顺序的原因,以上就是本文的全部内容,希望对大家的学习有所启发。

您可能有感兴趣的文章
ASP.NET中Response.BufferOutput属性的使用技巧

ASP.NET轻量级MVC框架Nancy的基本用法

ASP.NET Core中的对象池介绍

.NET集成ORM框架HiSql

asp.net中MVC的处理流程详解