Swagger UI - 使用 Microsoft.AspNet.WebApi.Versioning 时的 Web API 版本控制
Swagger UI - Web API versioning when using Microsoft.AspNet.WebApi.Versioning
我们在 Swagger 项目中使用 Web API 2。我的问题是,当 Microsoft.AspNet.WebApi.Versioning 应用如下时:
Swagger UI 忽略了一个事实,即现在我的 API 中有需要提供的版本。
我看了几个例子,但 none 似乎以令人满意的方式解决了这个问题。
如何强制 Swagger 让我添加 API 版本或仅将版本号自动添加到 URL?
到目前为止的 Swagger 配置:
GlobalConfiguration.Configuration
.EnableSwagger(c =>
{
c.SingleApiVersion("v1", "MoovShack.ServerApi");
// If your API has multiple versions, use "MultipleApiVersions" instead of "SingleApiVersion".
// In this case, you must provide a lambda that tells Swashbuckle which actions should be
// included in the docs for a given API version. Like "SingleApiVersion", each call to "Version"
// returns an "Info" builder so you can provide additional metadata per API version.
//
//c.MultipleApiVersions(
// (apiDesc, targetApiVersion) => ResolveVersionSupportByRouteConstraint(apiDesc, targetApiVersion),
// (vc) =>
// {
// vc.Version("v2", "Swashbuckle Dummy API V2");
// vc.Version("v1", "Swashbuckle Dummy API V1");
// });
c.OperationFilter<MoovShackTokenHeaderParameter>();
})
.EnableSwaggerUi(c =>
{
// If your API has multiple versions and you've applied the MultipleApiVersions setting
// as described above, you can also enable a select box in the swagger-ui, that displays
// a discovery URL for each version. This provides a convenient way for users to browse documentation
// for different API versions.
//
//c.EnableDiscoveryUrlSelector();
});
您可以看到到目前为止 MultipleApiVersions 被禁用 - 一个很好的原因是它不会产生任何结果。特别是因为我不确定“ResolveVersionSupportByRouteConstraint”应该做什么。
我还读到“EnableDiscoveryUrlSelector”有某种影响,但我也不确定这是否适用于我的情况。当我启用它时,没有任何反应。
我们在项目中是这样使用的,swagger可以识别,看起来不错
[ApiVersion( "1.0" )]
[Route("api/v{version:apiVersion}/[controller]")]
public class SomeControlelr: Controller{
[HttpGet("", Name = "Someaction"), MapToApiVersion("1.0")]
public async Task<IActionResult> SomeAction(string someParameter)
我们在 Swagger 项目中使用 Web API 2。我的问题是,当 Microsoft.AspNet.WebApi.Versioning 应用如下时:
Swagger UI 忽略了一个事实,即现在我的 API 中有需要提供的版本。
我看了几个例子,但 none 似乎以令人满意的方式解决了这个问题。
如何强制 Swagger 让我添加 API 版本或仅将版本号自动添加到 URL?
到目前为止的 Swagger 配置:
GlobalConfiguration.Configuration
.EnableSwagger(c =>
{
c.SingleApiVersion("v1", "MoovShack.ServerApi");
// If your API has multiple versions, use "MultipleApiVersions" instead of "SingleApiVersion".
// In this case, you must provide a lambda that tells Swashbuckle which actions should be
// included in the docs for a given API version. Like "SingleApiVersion", each call to "Version"
// returns an "Info" builder so you can provide additional metadata per API version.
//
//c.MultipleApiVersions(
// (apiDesc, targetApiVersion) => ResolveVersionSupportByRouteConstraint(apiDesc, targetApiVersion),
// (vc) =>
// {
// vc.Version("v2", "Swashbuckle Dummy API V2");
// vc.Version("v1", "Swashbuckle Dummy API V1");
// });
c.OperationFilter<MoovShackTokenHeaderParameter>();
})
.EnableSwaggerUi(c =>
{
// If your API has multiple versions and you've applied the MultipleApiVersions setting
// as described above, you can also enable a select box in the swagger-ui, that displays
// a discovery URL for each version. This provides a convenient way for users to browse documentation
// for different API versions.
//
//c.EnableDiscoveryUrlSelector();
});
您可以看到到目前为止 MultipleApiVersions 被禁用 - 一个很好的原因是它不会产生任何结果。特别是因为我不确定“ResolveVersionSupportByRouteConstraint”应该做什么。
我还读到“EnableDiscoveryUrlSelector”有某种影响,但我也不确定这是否适用于我的情况。当我启用它时,没有任何反应。
我们在项目中是这样使用的,swagger可以识别,看起来不错
[ApiVersion( "1.0" )]
[Route("api/v{version:apiVersion}/[controller]")]
public class SomeControlelr: Controller{
[HttpGet("", Name = "Someaction"), MapToApiVersion("1.0")]
public async Task<IActionResult> SomeAction(string someParameter)