zoukankan      html  css  js  c++  java
  • WebApi路由解析增加版本控制

    1.自定义路由解析类

    public class VersionHttpControllerSelector : IHttpControllerSelector
        {
            private const string CompanyKey = "company";
            private const string VersionKey = "version";
            private const string ControllerKey = "controller";
    
            private readonly HttpConfiguration _configuration;
            private readonly Lazy<Dictionary<string, HttpControllerDescriptor>> _controllers;
            private readonly HashSet<string> _duplicates;
    
            public VersionHttpControllerSelector(HttpConfiguration config)
            {
                _configuration = config;
                _duplicates = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
                _controllers = new Lazy<Dictionary<string, HttpControllerDescriptor>>(InitializeControllerDictionary);
            }
    
            private Dictionary<string, HttpControllerDescriptor> InitializeControllerDictionary()
            {
                var dictionary = new Dictionary<string, HttpControllerDescriptor>(StringComparer.OrdinalIgnoreCase);
    
                // Create a lookup table where key is "namespace.controller". The value of "namespace" is the last
                // segment of the full namespace. For example:
                // MyApplication.Controllers.V1.ProductsController => "V1.Products"
                IAssembliesResolver assembliesResolver = _configuration.Services.GetAssembliesResolver();
                IHttpControllerTypeResolver controllersResolver = _configuration.Services.GetHttpControllerTypeResolver();
    
                ICollection<Type> controllerTypes = controllersResolver.GetControllerTypes(assembliesResolver);
    
                foreach (Type t in controllerTypes)
                {
                    var segments = t.Namespace.Split(Type.Delimiter);
    
                    // For the dictionary key, strip "Controller" from the end of the type name.
                    // This matches the behavior of DefaultHttpControllerSelector.
                    var controllerName = t.Name.Remove(t.Name.Length - DefaultHttpControllerSelector.ControllerSuffix.Length);
                    string version = segments[segments.Length - 1];
                    string company = segments[segments.Length - 2];
                    var key = String.Format(CultureInfo.InvariantCulture, "{0}.{1}.{2}", company, version, controllerName);
                    if (version == "Controllers")
                    {
                         key = String.Format(CultureInfo.InvariantCulture, "{0}",  controllerName);
                    }            
                    // Check for duplicate keys.
                    if (dictionary.Keys.Contains(key))
                    {
                        _duplicates.Add(key);
                    }
                    else
                    {
                        dictionary[key] = new HttpControllerDescriptor(_configuration, t.Name, t);  
                    }
                }
    
                // Remove any duplicates from the dictionary, because these create ambiguous matches. 
                // For example, "Foo.V1.ProductsController" and "Bar.V1.ProductsController" both map to "v1.products".
                foreach (string s in _duplicates)
                {
                    dictionary.Remove(s);
                }
                return dictionary;
            }
    
            // Get a value from the route data, if present.
            private static T GetRouteVariable<T>(IHttpRouteData routeData, string name)
            {
                object result = null;
                if (routeData.Values.TryGetValue(name, out result))
                {
                    return (T)result;
                }
                return default(T);
            }
    
            public HttpControllerDescriptor SelectController(HttpRequestMessage request)
            {
                IHttpRouteData routeData = request.GetRouteData();
                if (routeData == null)
                {
                    throw new HttpResponseException(HttpStatusCode.NotFound);
                }
    
                // Get the company version and controller variables from the route data.
                string company = GetRouteVariable<string>(routeData, CompanyKey);
                
                string version = GetRouteVariable<string>(routeData, VersionKey);
                if (string.IsNullOrEmpty(version))
                {
                    version = GetVersionFromHTTPHeaderAndAcceptHeader(request);
                }
                string controllerName = GetRouteVariable<string>(routeData, ControllerKey);
                if (controllerName == null)
                {
                    throw new HttpResponseException(HttpStatusCode.NotFound);
                }
    
                // Find a matching controller.
                string key = String.Format(CultureInfo.InvariantCulture, "{0}",  controllerName);
                if (!string.IsNullOrEmpty(version) && !string.IsNullOrEmpty(company))
                {
                    key = String.Format(CultureInfo.InvariantCulture, "{0}.{1}.{2}", company, version, controllerName);
                }
    
                HttpControllerDescriptor controllerDescriptor;
                if (_controllers.Value.TryGetValue(key, out controllerDescriptor))
                {
                    return controllerDescriptor;
                }
                else if (_duplicates.Contains(key))
                {
                    throw new HttpResponseException(
                        request.CreateErrorResponse(HttpStatusCode.InternalServerError,
                        "Multiple controllers were found that match this request."));
                }
                else
                {
                    throw new HttpResponseException(HttpStatusCode.NotFound);
                }
            }
    
            public IDictionary<string, HttpControllerDescriptor> GetControllerMapping()
            {
                return _controllers.Value;
            }
            private string GetVersionFromHTTPHeaderAndAcceptHeader(HttpRequestMessage request)
            {
                if (request.Headers.Contains(VersionKey))
                {
                    var versionHeader = request.Headers.GetValues(VersionKey).FirstOrDefault();
                    if (versionHeader != null)
                    {
                        return versionHeader;
                    }
                }
                var acceptHeader = request.Headers.Accept;
                foreach (var mime in acceptHeader)
                {
                    if (mime.MediaType == "application/json" || mime.MediaType == "text/html")
                    {
                        var version = mime.Parameters
                                         .Where(v => v.Name.Equals(VersionKey, StringComparison.OrdinalIgnoreCase))
                                          .FirstOrDefault();
    
                        if (version != null)
                        {
                            return version.Value;
                        }
                        return string.Empty;
                    }
                }
                return string.Empty;
            }
        }
    

     2.注册路由及替换默认的IHttpControllerSelector

    public static class WebApiConfig
        {
            public static void Register(HttpConfiguration config)
            {
                config.Routes.MapHttpRoute(
                      name: "DefaultVersion",
                      routeTemplate: "{company}/{version}/{controller}/{id}",
                      defaults: new { id = RouteParameter.Optional }
                  );
    
                config.Services.Replace(typeof(IHttpControllerSelector), new VersionHttpControllerSelector((config)));
            }
        }
    
  • 相关阅读:
    LINUX操作系统VIM的安装和配置
    Ubuntu 14.04 LTS中怎样安装fcitx中文输入法
    Ubuntu 速配指南:开启3D桌面特效
    在U盘上安装Windows 7的详细步骤
    设置ip地址、掩码、网关、DNS
    U盘安装电脑系统教程
    取消word中所有超链接
    dos命令批处理发送文字到剪贴板
    Word2007:如何在竖版(纵向)页面中间插入横版(横向)页面
    ubuntu12.04 alternate win7 双系统安装
  • 原文地址:https://www.cnblogs.com/hyl8218/p/7357412.html
Copyright © 2011-2022 走看看