swagger 动态设置版本号_如何动态实现api版本控制和swagger文档

I am working in dotnet core api. I have to implement versioning on api. and swagger document should be categorized by api version.

解决方案

In .NetCore api versioning can be implement by adding below reference from nuget

Microsoft.AspNetCore.Mvc.Versioning

Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer

After adding reference do following in startup file of your project. Add below line before AddMvc line. I will use Header-api versioning. It means client will mention the version in header. Header name is customizable.

services.AddApiVersioning(this.Configuration);

Definition of AddApiVersioning would be like as (In different extension class):

public static void AddApiVersioning(this IServiceCollection services, IConfiguration configuration)

{

services.AddApiVersioning(apiVersioningOptions =>

{

apiVersioningOptions.ApiVersionReader = new HeaderApiVersionReader(new string[] { "api-version" }); // It means version will be define in header.and header name would be "api-version".

apiVersioningOptions.AssumeDefaultVersionWhenUnspecified = true;

var apiVersion = new Version(Convert.ToString(configuration["DefaultApiVersion"]));

apiVersioningOptions.DefaultApiVersion = new ApiVersion(apiVersion.Major, apiVersion.Minor);

apiVersioningOptions.ReportApiVersions = true;

apiVersioningOptions.UseApiBehavior = true; // It means include only api controller not mvc controller.

apiVersioningOptions.Conventions.Controller().HasApiVersion(apiVersioningOptions.DefaultApiVersion);

apiVersioningOptions.Conventions.Controller().HasApiVersion(apiVersioningOptions.DefaultApiVersion);

apiVersioningOptions.ApiVersionSelector = new CurrentImplementationApiVersionSelector(apiVersioningOptions);

});

services.AddVersionedApiExplorer(); // It will be used to explorer api versioning and add custom text box in swagger to take version number.

}

Here configuration["DefaultApiVersion"] is a key in appsetting having value 1.0

As in above code we have used Convention to define api version for each controller. It is useful when there is one api version and you don't want to label each controller with [ApiVersion] attribute.

If you don't want to use the Convention menthod to define version of controller. use attribute label to define version. like as below:

[Route("[controller]")]

[ApiController]

[ApiVersion("1.0")]

public class TenantController : ConfigController

Once this done go to StartUp file and add below code.

app.UseApiVersioning(); //Here app is IApplicationBuilder

That is complete solution for api versioning.

For swagger We have to add nuget package as defined below:

Swashbuckle.AspNetCore

Swashbuckle.AspNetCore.SwaggerGen

Swashbuckle.AspNetCore.SwaggerUI

After adding reference do below: Add below line after Services.UseApiVersioning()

services.AddSwaggerGenerationUI();

The definition of AddSwaggerGenerationUI is below in extens :

public static void AddSwaggerGenerationUI(this IServiceCollection services)

{

var provider = services.BuildServiceProvider()

.GetRequiredService();

services.AddSwaggerGen(action =>

{

action.OrderActionsBy(orderBy => orderBy.HttpMethod);

action.UseReferencedDefinitionsForEnums();

foreach (var item in provider.ApiVersionDescriptions)

{

action.SwaggerDoc(item.GroupName, new Swashbuckle.AspNetCore.Swagger.Info

{

Title = "Version-" + item.GroupName,

Version = item.ApiVersion.MajorVersion.ToString() + "." + item.ApiVersion.MinorVersion

});

}

});

}

This code will add swagger in pipeline. Now we have to use swagger. do below code in startup file.:

app.UseSwaggerGenerationUI(this.Configuration)

Definition of UseSwaggerGenerationUI would be like as :

public static void UseSwaggerGenerationUI(this IApplicationBuilder applicationBuilder, IApiVersionDescriptionProvider apiVersionDescriptionProvider, IConfiguration configuration)

{

applicationBuilder.UseSwagger(c =>

{

c.RouteTemplate = "/api/help/versions/{documentname}/document.json";

c.PreSerializeFilters.Add((swaggerDoc, httpReq) => swaggerDoc.BasePath = "/api");

});

applicationBuilder.UseSwaggerUI(c =>

{

c.RoutePrefix = "api/help";

c.DocumentTitle = "Api Help";

foreach (var item in apiVersionDescriptionProvider.ApiVersionDescriptions)

{

c.SwaggerEndpoint($"/api/help/versions/{item.GroupName}/document.json", item.GroupName);

}

});

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值