簡體   English   中英

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 中有需要提供的版本。

我看了幾個例子,但似乎沒有一個能以令人滿意的方式解決這個問題。

如何強制 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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM