C# Web API 版本控制

C# Web API Versioning

我花了几天时间尝试让 C# Web API header 版本控制正常工作;并且现在它调用了一个不同的命名空间,例如v1,v2 但是当我尝试使用诸如 id 之类的参数发出请求时它不起作用,而是调用默认的网络 api 控制器方法。

下面显示了所有代码,因此非常希望能帮助您理解如何使用 guid 调用方法,而不仅仅是默认的空方法。

下面是我正在使用的 NamespaceControllerSelector。我通过 WebApi.Config.cs 文件添加它并添加 Replace 方法调用,如下所示。

config.Services.Replace(typeof(IHttpControllerSelector), new NamespaceControllerSelector(config));

控制器选择器

public class NamespaceControllerSelector : IHttpControllerSelector
    {
        //private const string NamespaceKey = "namespace";
        private const string ControllerKey = "controller";

        private readonly HttpConfiguration _configuration;
        private readonly Lazy<Dictionary<string, HttpControllerDescriptor>> _controllers;
        private readonly HashSet<string> _duplicates;

        public NamespaceControllerSelector(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);

                var key = String.Format(CultureInfo.InvariantCulture, "{0}.{1}", segments[segments.Length - 1], 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 namespace and controller variables from the route data.
            string namespaceName = "v" + GetVersionFromHttpHeader(request);//GetRouteVariable<string>(routeData, NamespaceKey);
            if (namespaceName == null)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }

            string controllerName = GetControllerName(request);//GetRouteVariable<string>(routeData, ControllerKey);
            if (controllerName == null)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }

            // Find a matching controller.
            string key = String.Format(CultureInfo.InvariantCulture, "{0}.{1}", namespaceName, 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 GetVersionFromHttpHeader(HttpRequestMessage request)
        {
            const string headerName = "version";

            if (request.Headers.Contains(headerName))
            {
                var versionHeader = request.Headers.GetValues(headerName).FirstOrDefault();
                if (versionHeader != null)
                {
                    return versionHeader;
                }
            }

            return string.Empty;
        }


        public virtual string GetControllerName(HttpRequestMessage request)
        {

            IHttpRouteData routeData = request.GetRouteData();
            if (routeData == null)
            {
                return null;
            }

            // Look up controller in route data
            object controllerName = null;
            routeData.Values.TryGetValue(ControllerKey, out controllerName);
            return (string)controllerName;
        }

    }

我调用的UsersController

namespace Api.Controllers.v1
{
    //[Authorize]
    /// <summary>
    /// User controller
    /// </summary>
    //[RoutePrefix("api")]
    public class UsersController : ApiController
    {
        /// <summary>
        /// Get all the users in the system
        /// </summary>
        /// <returns>A list of User objects</returns>
        //[Route("users")]
        [HttpGet]
        public HttpResponseMessage GetUsers()
        {
            return Request.CreateResponse(HttpStatusCode.OK, DummyUsers.GetAllUsers());
        }

        /// <summary>
        /// Get a user by there id
        /// </summary>
        /// <param name="userId">The users id</param>
        /// <returns>A User object, will return 404 (Not Found) if unable to find</returns>
        //[Route("users/{userId:guid}")]
        [HttpGet]
        public HttpResponseMessage GetUsers(Guid userId)
        {
            User user = DummyUsers.GetAllUsers().FirstOrDefault(x => x.Id == userId);
            if (user != null)
            {
                return Request.CreateResponse(HttpStatusCode.OK, user);
            }
            return Request.CreateResponse(HttpStatusCode.NotFound);
        }

    }
}

我正在提出的请求,如您所见,我在 api/users 之后传递了 guid,因此希望它调用 GetUsers(Guid guid) 方法,但它只是调用 GetUsers() 方法。需要帮助吗?

GET http://localhost:62249/api/users/d4eb9dd9-571a-406b-9b6d-07154293c21d
Host: localhost:62249
Connection: keep-alive
Version: 1
Accept: application/json
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8

非常感谢安德鲁

问题是

GetUsers(Guid guid) 

应该是

GetUsers(Guid id)

可能是一个业余错误,但这是一个新手,因为它被认为足够清楚。对于一些带有额外参数的 get 方法,路由似乎有点问题。