當(dāng)前位置:首頁(yè) >  站長(zhǎng) >  編程技術(shù) >  正文

netcorewebapi多版本控制與swagger(nswag)配置教程

 2020-11-20 15:17  來(lái)源: 腳本之家   我來(lái)投稿 撤稿糾錯(cuò)

  域名預(yù)訂/競(jìng)價(jià),好“米”不錯(cuò)過(guò)

這篇文章主要介紹了net core webapi多版本控制與*ger(n*)配置,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

目錄

前言

版本控制

前言

首先希望webapi支持多版本,*ger針對(duì)不同的版本可進(jìn)行交互。多版本控制基于Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer包,*ger可以選擇Swashbuckle.AspNetCore和n*.AspNetCore.由于我們系統(tǒng)使用的是n*所以繼續(xù)沿用,當(dāng)然Swashbuckle.AspNetCore也和不錯(cuò),有時(shí)間再總結(jié)。

版本控制

1.導(dǎo)入相關(guān)nuget。Swashbuckle.AspNetCore,n*.AspNetCore.

2.添加api多版本控制服務(wù)

2.1.首先是讓項(xiàng)目支持多版本的服務(wù)添加

services.AddApiVersioning(option =>
  {
   // 可選,為true時(shí)API返回支持的版本信息
   option.ReportApiVersions = true;
   // 不提供版本時(shí),默認(rèn)為1.0
   option.AssumeDefaultVersionWhenUnspecified = true;
   //版本信息放到header ,不寫在不配置路由的情況下,版本信息放到response url 中
   option.ApiVersionReader = new HeaderApiVersionReader("api-version");
   // 請(qǐng)求中未指定版本時(shí)默認(rèn)為1.0
   option.DefaultApiVersion = new ApiVersion(1, 0);
  }).AddVersionedApiExplorer(option =>
  {  // 版本名的格式:v+版本號(hào)
   option.GroupNameFormat = "'v'V";
   option.AssumeDefaultVersionWhenUnspecified = true;
  });
  ////獲取webapi版本信息,用于*ger多版本支持
  this.provider = services.BuildServiceProvider().GetRequiredService<IApiVersionDescriptionProvider>();

服務(wù)我們已經(jīng)注入了,下面我們看一下怎么webapi多版本的支持

2.1.1.多版本的控制

1.QueryString

/// <summary>
 /// 用戶管理API
 /// </summary>
 [ServiceFilter(typeof(LogFilterAttribute))]
 [ApiController]
 [Route("api/[controller]/[action]")]
 [ApiVersion("2.0")]
 
 public class UserController : ApiController
 {}

當(dāng)我們注冊(cè)服務(wù)時(shí)不加 option.ApiVersionReader = new HeaderApiVersionReader("api-version");那么版本信息就是通過(guò)url?api-version=2進(jìn)行傳遞2.header

2.header

/// <summary>
 /// 用戶管理API
 /// </summary>
 [ServiceFilter(typeof(LogFilterAttribute))]
 [ApiController]
 [Route("api/[controller]/[action]")]
 [ApiVersion("2.0")]
 
 public class UserController : ApiController
 {}

如果不指定版本路由那么定義ApiVersionReader 則通過(guò)header傳遞

以上兩種方式,默認(rèn)版本(v1.0)均可不傳遞版本號(hào)

3.版本路由

/// <summary>
 /// 用戶管理API
 /// </summary>
 [ServiceFilter(typeof(LogFilterAttribute))]
 [ApiController]
 [Route("api/v{version:apiVersion}/[controller]/[action]")]
 [Authorize]
 [ApiVersion("1.0")]
 [ApiVersion("2.0")]
 public class UserController : ApiController
 {}

這種方式很直觀,但如果原有項(xiàng)目沒(méi)有使用多版本控制不建議用,可采用header的方式更為合理一些,

2.1.2同一個(gè) Controller支持多版本

增加多個(gè) [ApiVersion("2.0")]即可。

/// <summary>
 /// 用戶管理API
 /// </summary>
 [ServiceFilter(typeof(LogFilterAttribute))]
 [ApiController]
 [Route("api/v{version:apiVersion}/[controller]/[action]")]
 //[Authorize]
 [ApiVersion("1.0")]
 [ApiVersion("2.0")]
 public class UserController : ApiController
 {}

但是兩個(gè)相同的版本中Controller不能有相同的方法。比如v1文件夾和v2文件的UserController都指向v2版本,是不能同時(shí)擁有GetList()的,但是如果我們想要v2中的GetList重寫v1的GetList方法,其他的方法都繼承過(guò)來(lái)怎么處理呢?

v1版本中的controller指定[ApiVersion("1.0")][ApiVersion("2.0")]

/// <summary>
 /// v1.用戶管理API
 /// </summary>
 [ServiceFilter(typeof(LogFilterAttribute))]
 [ApiController]
 [Route("api/v{version:apiVersion}/[controller]/[action]")]
 //[Authorize]
 [ApiVersion("1.0")]
 [ApiVersion("2.0")]
 public class UserController : ApiController
 {}

v2版本中的controller指定[ApiVersion("2.0")]

/// <summary>
 /// v1.用戶管理API
 /// </summary>
 [ServiceFilter(typeof(LogFilterAttribute))]
 [ApiController]
 [Route("api/v{version:apiVersion}/[controller]/[action]")]
 //[Authorize]
 [ApiVersion("2.0")]
 public class UserController : ApiController
 {}

v1版本中的GetList()方法 MapToApiVersion到v1即可

/// <summary>
 /// 獲取用戶列表
 /// </summary>
 /// <returns></returns>
 [HttpGet,MapToApiVersion("1.0")]
 public NetResponse<List<User>> GetList()
 {}

這樣以來(lái)v1與v2中的GetList就互不影響了。

3.注冊(cè)n*(AddOpenApiDocument和AddSwaggerDocument)

NSwag注入服務(wù)有兩個(gè)方法:AddOpenApiDocument和AddSwaggerDocument,兩者的區(qū)別就是架構(gòu)類型不一樣,AddOpenApiDocument的SchemaType使用的是OpenApi3,AddSwaggerDocument的SchemaType使用的是Swagger2:

我用的是AddSwaggerDocument

foreach (var description in provider.ApiVersionDescriptions)
  {
   services.AddSwaggerDocument(document =>
  {
   document.OperationProcessors.Add(new OperationSecurityScopeProcessor("JWT token"));
   document.DocumentName = description.GroupName;
   document.Version = description.GroupName;
   document.ApiGroupNames = new string[] { description.GroupName };
   //jwt 認(rèn)證
   document.AddSecurity("JWT token", Enumerable.Empty<string>(),
    new OpenApiSecurityScheme()
    {
    Type = OpenApiSecuritySchemeType.ApiKey,
    Name = nameof(Authorization),
    In = OpenApiSecurityApiKeyLocation.Header,
    Description = "將token值復(fù)制到如下格式: \nBearer {token}"
    }
   );

  });
  }

4,n*中間件

app.UseOpenApi();
  app.UseSwaggerUi3(setting =>
  {
  });

是的我們做任何配置,如果你愿意其實(shí)有很多好玩的。但上面的配置方式足夠多版本的控制與n*交互。

到此這篇關(guān)于net core webapi多版本控制與*ger(n*)配置教程的文章就介紹到這了,更多相關(guān)net core webapi多版本控制內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

來(lái)源:腳本之家

鏈接:https://www.jb51.net/article/198889.htm

申請(qǐng)創(chuàng)業(yè)報(bào)道,分享創(chuàng)業(yè)好點(diǎn)子。點(diǎn)擊此處,共同探討創(chuàng)業(yè)新機(jī)遇!

相關(guān)標(biāo)簽
asp.net
net教程

相關(guān)文章

熱門排行

信息推薦