• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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

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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
在IIS上部署你的ASP.NETCore项目发布时间:2022-07-10
下一篇:
Asp.netmvc中Controller的返回值发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap