我正在尝试在REST API上启用版本控制,其中版本在标题中指定为"api-version":2
,供应商媒体类型,"accept: application/vnd.company.resource-v2+json
,application/json; version=2"
或在查询字符串"?version=2"
。实现使用IHttpRouteConstraint和RouteFactoryAttribute。这非常有效。但是,Swagger无法将正确的模型与正确的版本化文档相匹配。 operationId始终来自版本1模型。
public class DevicesController : ApiController
{
[HttpGet, RouteVersion( "api/devices", 1, Name = "v1.devices" )]
[ResponseType( typeof( IEnumerable<Models.V1.Device> ) )]
public IHttpActionResult GetV1( )
{
return Ok( new List<Models.V1.Device> { ... } );
}
[HttpGet, RouteVersion( "api/devices", 2, Name = "v2.devices" )]
[ResponseType( typeof( IEnumerable<Models.V2.Device> ) )]
public IHttpActionResult GetV2( )
{
return Ok( new List<Models.V2.Device> { ... } );
}
}
V2的Swagger文档具有错误的operationId和MIME类型。
"tags":
[
"Devices"
],
"summary": "Get a list of device(s) by the device identifier",
"operationId": "Devices_GetV1",
"consumes": [ ],
"produces":
[
"application/vnd.safe.inventory-v1+json",
"application/json",
"text/json",
"application/vnd.safe.inventory-v1+xml",
"application/xml",
"text/xml"
],
...
Swashbuckle 5.2.2
我已尝试自定义ApiExplorer,如以下帖子中所述:
github.com/domaindrivendev/Swashbuckle/issues/558
github.com/domaindrivendev/Swashbuckle/issues/317
我也尝试过构建自定义选择器。
我也走了这条路,但我认为这不是同一个问题。 https://azure.microsoft.com/en-us/documentation/articles/app-service-api-dotnet-swashbuckle-customize/
向任何方向询问任何帮助将不胜感激。
提前致谢。
答案 0 :(得分:1)
尝试添加Microsoft.AspNet.WebApi.Versioning.ApiExplorer
并使用apiExplorer
,如下所示:https://github.com/Microsoft/aspnet-api-versioning/wiki/API-Documentation
这对我有用。
答案 1 :(得分:0)
我们找到了一个自定义ApiExplorer的解决方案,如http://blog.ulriksen.net/versioned-iapiexplorer
中所述在Swagger配置中:使用MultipleApiVersions
c.MultipleApiVersions( ( apiDesc, version ) =>
{
var versionConstraint = apiDesc.Route.Constraints[ "version" ] as RouteVersionConstraint;
return ( versionConstraint != null ) && versionConstraint.AllowedVersion == version.ConvertTo<int>( );
},
vc =>
{
vc.Version( "2", "API V2" ); //add this line when v2 is released
vc.Version( "1", "API V1" );
} );