Swagger 扬眉吐气
我试图在REST API上启用版本控制,其中版本在标题中指定为Swagger 扬眉吐气,swagger,Swagger,我试图在REST API上启用版本控制,其中版本在标题中指定为“API版本”:2,供应商媒体类型,接受:application/vnd.company.resource-v2+json,应用程序/json;版本=2“,或在查询字符串?版本=2”。该实现使用IHttpRouteConstraint和RouteFactoryAttribute。这很好用。但是,Swagger无法将正确的模型与正确版本的文档相匹配。operationId始终来自版本1型号 public class DevicesCon
“API版本”:2
,供应商媒体类型,接受:application/vnd.company.resource-v2+json,应用程序/json;版本=2“
,或在查询字符串?版本=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> { ... } );
}
}
虚张声势5.2.2
我已尝试按照以下帖子中的描述定制ApiExplorer:
github.com/domaindrivendev/Swashbuckle/issues/558
github.com/domaindrivendev/Swashbuckle/issues/317
我还尝试构建一个自定义选择器
我也走了这条路,但我不认为这是同一个问题。
如果您需要任何指导和帮助,我们将不胜感激
提前感谢。我们找到了一个使用自定义ApiExplorer的解决方案,如中所述 在招摇过市配置中:使用多个分段
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" );
} );
c.multiplediversions((apiDesc,版本)=>
{
var versionConstraint=apiDesc.Route.Constraints[“version”]作为RouteVersionConstraint;
return(versionConstraint!=null)和&versionConstraint.AllowedVersion==version.ConvertTo();
},
vc=>
{
Version(“2”,“APIv2”);//在V2发布时添加此行
vc.版本(“1”、“API V1”);
} );
尝试添加Microsoft.AspNet.WebApi.Versioning.ApiExplorer
并使用ApiExplorer
,如下所述:
这对我很有用。你好,请问您的RouteVersionContraint课程是什么?
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" );
} );