关闭 x
IT技术网
    技 采 号
    ITJS.cn - 技术改变世界
    • 实用工具
    • 菜鸟教程
    IT采购网 中国存储网 科技号 CIO智库

    IT技术网

    IT采购网
    • 首页
    • 行业资讯
    • 系统运维
      • 操作系统
        • Windows
        • Linux
        • Mac OS
      • 数据库
        • MySQL
        • Oracle
        • SQL Server
      • 网站建设
    • 人工智能
    • 半导体芯片
    • 笔记本电脑
    • 智能手机
    • 智能汽车
    • 编程语言
    IT技术网 - ITJS.CN
    首页 » .NET »给ASP.NET MVC及WebApi添加路由优先级

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

    2015-07-09 00:00:00 出处:ITJS
    分享

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

    大家都知道我们在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 Mvc的一个很小的功能拓展,小项目可能不太需要这个功能,但有时候项目大了注册的路由不生效时你应该要想到有可能是因为路由顺序的原因,这时这个路由优先级的功能有可能就会给你带来便利。总之共享给有需要的朋友们参考。

    上一篇返回首页 下一篇

    声明: 此文观点不代表本站立场;转载务必保留本文链接;版权疑问请联系我们。

    别人在看

    Destoon 模板存放规则及语法参考

    Destoon系统常量与变量

    Destoon系统目录文件结构说明

    Destoon 系统安装指南

    Destoon会员公司主页模板风格添加方法

    Destoon 二次开发入门

    Microsoft 将于 2026 年 10 月终止对 Windows 11 SE 的支持

    Windows 11 存储感知如何设置?了解Windows 11 存储感知开启的好处

    Windows 11 24H2 更新灾难:系统升级了,SSD固态盘不见了...

    小米路由器买哪款?Miwifi热门路由器型号对比分析

    IT头条

    Synology 对 Office 套件进行重大 AI 更新,增强私有云的生产力和安全性

    01:43

    StorONE 的高效平台将 Storage Guardian 数据中心占用空间减少 80%

    11:03

    年赚千亿的印度能源巨头Nayara 云服务瘫痪,被微软卡了一下脖子

    12:54

    国产6nm GPU新突破!砺算科技官宣:自研TrueGPU架构7月26日发布

    01:57

    公安部:我国在售汽车搭载的“智驾”系统都不具备“自动驾驶”功能

    02:03

    技术热点

    如何删除自带的不常用应用为windows 7减负

    MySQL中多表删除方法

    改进的二值图像像素标记算法及程序实现

    windows 7 32位系统下手动修改磁盘属性例如M盘修改为F盘

    windows 7中怎么样在家庭组互传文件

    Linux应用集成MySQL数据库访问技巧

      友情链接:
    • IT采购网
    • 科技号
    • 中国存储网
    • 存储网
    • 半导体联盟
    • 医疗软件网
    • 软件中国
    • ITbrand
    • 采购中国
    • CIO智库
    • 考研题库
    • 法务网
    • AI工具网
    • 电子芯片网
    • 安全库
    • 隐私保护
    • 版权申明
    • 联系我们
    IT技术网 版权所有 © 2020-2025,京ICP备14047533号-20,Power by OK设计网

    在上方输入关键词后,回车键 开始搜索。Esc键 取消该搜索窗口。