使用URL在Web Api 2中进行控制器版本控制 [英] Controller Versioning in Web Api 2 using URL

查看:92
本文介绍了使用URL在Web Api 2中进行控制器版本控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对控制器使用基于URL的版本控制.我发现的最佳解决方案是下面的代码. 我正在为此寻找更好的解决方案.我尝试了约束它没有用 也许我做错了什么,我唯一关心的就是在不同的命名空间中使用具有相同名称的控制器...?! 我用字符串合并来创建想要的类型.可能这不是一个好主意. 如果您知道任何有关此主题的内容,请发送很好的参考.

I want to use URL-Based versioning for my controller. best solution that I found was code below. I'm looking for better solution for this. I tried Constrain It didn't work maybe i did some thing wrong my only concern is using controller with same name in different namespace...?! I userd string merging to creating wanted type. probably it is not a good idea. Please send good reference for this topic if you know any.....?

public class ControllerVersioning : DefaultHttpControllerSelector
    {
        private HttpConfiguration _config;
        public ControllerVersioning(HttpConfiguration config)
            : base(config)
        {
            _config = config;
        }

        public override HttpControllerDescriptor SelectController(HttpRequestMessage request)
        {
            var routeData = request.GetRouteData();

            var controllerName = routeData.Values["controller"].ToString();
            controllerName = char.ToUpper(controllerName[0]) + controllerName.Substring(1); 
            var versionName = routeData.Values["version"].ToString();

            HttpControllerDescriptor controllerDescriptor = new HttpControllerDescriptor();
            controllerDescriptor.Configuration = _config;
            controllerDescriptor.ControllerName = controllerName;

            string s = "ngolforoushan.Web.Api.Controllers.V" + versionName + "." + controllerName + "Controller";
            Type t=Type.GetType(s);
            controllerDescriptor.ControllerType = t;

            return controllerDescriptor;

        }
    }

推荐答案

var dictionary = new Dictionary<string, HttpControllerDescriptor>(StringComparer.OrdinalIgnoreCase);

    var assembliesResolver = _config.Services.GetAssembliesResolver();
    var controllerResolver = _config.Services.GetHttpControllerTypeResolver();

    var controllerTypes = controllerResolver.GetControllerTypes(assembliesResolver);

    foreach (var cType in controllerTypes)
    {
        var segments = cType.Namespace.Split(Type.Delimiter);

        var controllerName = cType.Name.Remove(cType.Name.Length - DefaultHttpControllerSelector.ControllerSuffix.Length);

        var controllerKey = String.Format(CultureInfo.InvariantCulture, "{0}.{1}", segments[segments.Length - 1], controllerName);
        if (!dictionary.Keys.Contains(controllerKey))
        {
            dictionary[controllerKey] = new HttpControllerDescriptor(_config, cType.Name, cType);
        }
    }

通过这种方法,您可以返回Assembly上所有控制器的列表并将它们全部放入字典中.

This is the way that you can return list of all controller on your Assembly and put all of them to a dictionary.

基于我上面的代码-我的意思是线程主题,我的第一篇文章-您知道如何获取{version,controller}并选择相关的控制器并将其作为参数传递.

based on my code above-I mean thread topic, my first post- you know how you can get {version,controller} and selected related controller and passing it as a parameter.

我在下面添加了代码,以更轻松地获取版本和控制器,但是您需要知道是否具有与版本无关的路由,这需要在分配版本和控制器字符串之前进行检查.

I added code below to make it easier to fetch version and controller but you need to know if you have different route which they are not related to versioning you need to check before assign version and controller strings.

config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{version}/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

这篇关于使用URL在Web Api 2中进行控制器版本控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆