net core webapi多版本控制與swagger(nswag)配置教程
前言
首先希望webapi支持多版本,swagger針對(duì)不同的版本可進(jìn)行交互。多版本控制基于Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer包,swagger可以選擇Swashbuckle.AspNetCore和nswag.AspNetCore.由于我們系統(tǒng)使用的是nswag所以繼續(xù)沿用,當(dāng)然Swashbuckle.AspNetCore也和不錯(cuò),有時(shí)間再總結(jié)。
版本控制
1.導(dǎo)入相關(guān)nuget。Swashbuckle.AspNetCore,nswag.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版本信息,用于swagger多版本支持 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
/// <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è)nswag(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,nswag中間件
app.UseOpenApi(); app.UseSwaggerUi3(setting => { });
是的我們做任何配置,如果你愿意其實(shí)有很多好玩的。但上面的配置方式足夠多版本的控制與nswag交互。
到此這篇關(guān)于net core webapi多版本控制與swagger(nswag)配置教程的文章就介紹到這了,更多相關(guān)net core webapi多版本控制內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
ASP.NET學(xué)習(xí)CORE中使用Cookie身份認(rèn)證方法
本篇文章主要給大家詳細(xì)分析了ASP.NET學(xué)習(xí)CORE中使用Cookie身份認(rèn)證方法以及相關(guān)的實(shí)例代碼,有需要的朋友參考下吧。2018-01-01asp.net實(shí)現(xiàn)在非MVC中使用Razor模板引擎的方法
這篇文章主要介紹了asp.net實(shí)現(xiàn)在非MVC中使用Razor模板引擎的方法,較為詳細(xì)的分析了Razor模板引擎的使用技巧,需要的朋友可以參考下2015-06-06asp.net Linq To Xml上手Descendants、Elements遍歷節(jié)點(diǎn)
C#3.0 Vs2008 RTM 本文介紹如何使用 Descendants、Elements快速遍歷XML節(jié)點(diǎn)2009-07-07ASP.NET WebAPI連接數(shù)據(jù)庫(kù)的方法
這篇文章主要為大家詳細(xì)介紹了ASP.NET WebAPI連接數(shù)據(jù)庫(kù)的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08[Asp.Net MVC4]驗(yàn)證用戶登錄實(shí)現(xiàn)實(shí)例
這篇文章主要介紹了[Asp.Net MVC4]驗(yàn)證用戶登錄實(shí)現(xiàn)實(shí)例,這里整理了詳細(xì)的代碼,具有一定的參考價(jià)值,有需要的小伙伴可以參考下。2016-12-12ASP.NET實(shí)現(xiàn)二維碼(QRCode)的創(chuàng)建和讀取實(shí)例
這篇文章主要介紹了ASP.NET實(shí)現(xiàn)二維碼(QRCode)的創(chuàng)建和讀取實(shí)例,分析了二維碼的實(shí)現(xiàn)原理與完整的代碼實(shí)現(xiàn)步驟,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-01-01ASP.NET頁(yè)面之間傳值的方式之Application實(shí)例詳解
這篇文章主要介紹了ASP.NET頁(yè)面之間傳值的方式之Application實(shí)例詳解,需要的朋友可以參考下2017-10-10如何ASP.NET Core Razor中處理Ajax請(qǐng)求
本篇技術(shù)文章主要給大家講述了如何ASP.NET Core Razor中處理Ajax請(qǐng)求這方面的知識(shí)點(diǎn),有興趣的朋友參考下。2018-01-01