簡體   English   中英

NSwag 和多個 api 版本

[英]NSwag and multiple api versions

考慮以下控制器:

namespace Web.Controllers
{
    [ApiVersioning("1.0")
    [Route("api/v{version:apiVersion}/[controller]")]
    public class Product : ApiController
    {
        [HttpGet("id")]
        public IHttpActionResult<bool> GetProduct(Guid id) 
        { /* rest of the code */ }
    }
}

namespace Web.Controllers
{
    [ApiVersioning("2.0")
    [Route("api/v{version:apiVersion}/[controller]")]
    public class Product2 : ApiController
    {
        [HttpGet("id")]
        public IHttpActionResult<bool> GetProduct(Guid id) 
        { /* rest of the code */ }
    }
}

以及Startup class 中的 Swagger 文檔:

services.AddSwaggerDocument(config =>
{
    config.DocumentName = "v1.0";
    config.PostProcess = document =>
    {
        document.Info.Version = "v1.0";
    };
});

services.AddSwaggerDocument(config =>
{
    config.DocumentName = "v2.0";
    config.PostProcess = document =>
    {
        document.Info.Version = "v2.0";
    };
});

現在,在使用NSwag測試 API 瀏覽器后,它會忽略版本並顯示 v1 和 v2 文檔中的所有 API。

如何告訴 NSwag 將它們分開?

我認為您缺少用於 select Api 版本的 ApiGroupNames 屬性。 請像下面的代碼一樣添加 ApiGroupNames 屬性並告訴我們。

services.AddSwaggerDocument(config =>
{
    config.DocumentName = "v1.0";
    config.PostProcess = document =>
    {
        document.Info.Version = "v1.0";
    };
    config.ApiGroupNames = new[] { "1.0" };
});

services.AddSwaggerDocument(config =>
{
    config.DocumentName = "v2.0";
    config.PostProcess = document =>
    {
        document.Info.Version = "v2.0";
    };
    config.ApiGroupNames = new[] { "2.0" };
});

暫無
暫無

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

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