给Asp.Net MVC及WebApi添加路由优先级

作者:湖北诺贝特科技有限公司  来源:www.nbt158.com未知  发布时间:2017-09-07 13:24:12
给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 RouteTemplateget;set; public object Defaultsget;set; public object Constraintsget;set; public HttpMessageHandler Handlerget;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) 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类中

企业建站2800元起,携手武汉肥猫科技,做一个有见地的颜值派!更多优惠请戳:十堰网站制作 http://shiyan.666rj.com


上一篇:Python网络编程08----Django模版
下一篇:最后一页