Swagger UI - 使用Microsoft.AspNet.WebApi.Versioning

时间:2017-11-09 14:23:27

标签: c# asp.net-web-api swagger swagger-ui

我们在 Swagger 的项目中使用 Web API 2 。我的问题是当 Microsoft.AspNet.WebApi.Versioning 应用如下:

enter image description here

Swagger UI 忽略了现在我的API中需要提供版本的事实。

我看了几个例子,但似乎没有人以令人满意的方式解决这个问题。

如何强制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 ”会产生某种影响,但我也不确定这是否适用于我的情况。当我启用它时,什么也没发生。

1 个答案:

答案 0 :(得分:1)

我们在项目中像这样使用它,并且大摇大摆地认出了它,看起来还不错

[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)