我扩展了
Michael Brown的答案,以允许设置默认版本:
现在我正在考虑如何使其与Swashbuckle swagger配合使用。
RouteVersionAttribute:
using System.Collections.Generic;
using System.Web.Http.Routing;
namespace YourNameSpace.Filters
{
public class RouteVersionAttribute : RouteFactoryAttribute
{
public int Version { get; private set; }
public int VersionDefault { get; private set; }
public RouteVersionAttribute() : this(null, 1, true)
{
}
public RouteVersionAttribute(int version, bool isDefault = false) : this(null, version, isDefault)
{
}
public RouteVersionAttribute(string template, int version, bool isDefault = false)
: base(template)
{
Version = version;
if (isDefault)
VersionDefault = version;
}
public override IDictionary<string, object> Constraints
{
get
{
var constraints = new HttpRouteValueDictionary();
constraints.Add("version", new RouteVersionHttpConstraint(Version, VersionDefault));
return constraints;
}
}
public override IDictionary<string, object> Defaults
{
get
{
var defaults = new HttpRouteValueDictionary();
defaults.Add("version", VersionDefault);
return defaults;
}
}
}
}
RouteVersionHttpConstraint:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Web.Http.Routing;
namespace Boyd.Core.Filters
{
public class RouteVersionHttpConstraint : IHttpRouteConstraint
{
public const string VersionHeaderName = "api-version";
private readonly int VersionDefault = 1;
public RouteVersionHttpConstraint(int allowedVersion, int versionDefault)
{
AllowedVersion = allowedVersion;
VersionDefault = versionDefault;
}
public int AllowedVersion
{
get;
private set;
}
public bool Match(HttpRequestMessage request, IHttpRoute route, string parameterName, IDictionary<string, object> values, HttpRouteDirection routeDirection)
{
if (routeDirection == HttpRouteDirection.UriResolution)
{
int version = GetVersionHeaderOrQuery(request) ?? VersionDefault;
if (version == AllowedVersion)
{
return true;
}
}
return false;
}
private int? GetVersionHeaderOrQuery(HttpRequestMessage request)
{
string versionAsString;
if (request.Headers.TryGetValues(VersionHeaderName, out IEnumerable<string> headerValues)
&& headerValues.Count() == 1)
{
versionAsString = headerValues.First();
if (versionAsString != null && Int32.TryParse(versionAsString, out int version))
{
return version;
}
}
else
{
var query = System.Web.HttpUtility.ParseQueryString(request.RequestUri.Query);
string versionStr = query[VersionHeaderName];
int.TryParse(versionStr, out int version);
if (version > 0)
return version;
}
return null;
}
}
}
用法(可用于控制器或操作方法):
#region Temporary Tests
[HttpGet]
[RouteVersion("Test", 1)]
public async Task<IHttpActionResult> Test1([FromBody]GetCustomerW2GsForPropertyRequest request)
{
return await Task.FromResult(Ok("API Version 1 selected"));
}
[HttpGet]
[RouteVersion("Test", 2)]
[RouteVersion("Test", 3)]
[RouteVersion("Test", 4)]
public async Task<IHttpActionResult> Test4([FromBody]GetCustomerW2GsForPropertyRequest request)
{
return await Task.FromResult(Ok("API Version 2, 3 or 4 selected"));
}
[HttpGet]
[RouteVersion("Test", 5, true)]
public async Task<IHttpActionResult> Test5([FromBody]GetCustomerW2GsForPropertyRequest request)
{
return await Task.FromResult(Ok("API Version 5 selected"));
}
#endregion Temporary Tests