net5 webapi中 SwaggerUI如何进行版本控制
创建项目
net5就自带上了swaggerUI,见红色
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{ services.AddControllers();
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "Net5.WebAPI", Version = "v1" });
});
} // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "Net5.WebAPI v1"));
} app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
代码改造
1.添加FirstController
public class FirstController : ControllerBase
{
/// <summary>
/// 这是V1版本的GetString
/// </summary>
/// <returns></returns>
[HttpGet]
[Route("api/[controller]/GetString")]
public string GetToString()
{
return Newtonsoft.Json.JsonConvert.SerializeObject(new
{
Id = 123,
Name = "闪电五连鞭"
});
} /// <summary>
/// 这是V1版本的GetString002
/// </summary>
/// <returns></returns>
[HttpGet]
[Route("api/[controller]/GetString002")]
public string GetString002()
{
return Newtonsoft.Json.JsonConvert.SerializeObject(new
{
Id = 123,
Name = "闪电五连鞭"
});
} /// <summary>
/// 这是V1版本的Add
/// </summary>
/// <returns>name</returns>
[HttpPost]
[Route("api/[controller]/Add/{name:required}")]
public string Add(string name)
{
return Newtonsoft.Json.JsonConvert.SerializeObject(new
{
Id = 234,
Name = name
});
} [HttpPut]
[Route("api/[controller]/Update/{id:int}")]
public int Update(int Id)
{
return Id;
}
[Route("api/[controller]/Update/{id:int}")]
[HttpDelete]
public int Delete(int Id)
{
return Id;
} [Route("api/[controller]/Patch")]
[HttpPatch]
public int Patch()
{
return 123;
}
}
生成swagge有关项目的Xml注释文件,并修改其属性为“始终复制”
添加版本枚举类
public enum ApiVersions
{
V1 = 1,
V2 = 2,
V3 = 3,
V4 = 4,
V5 = 5
}
改造StartUp中swagger代码
public void ConfigureServices(IServiceCollection services)
{ services.AddControllers();
services.AddSwaggerGen(c =>
{
typeof(ApiVersions).GetEnumNames().ToList().ForEach(version =>
{
c.SwaggerDoc(version, new OpenApiInfo()
{
Title = "Net5.WebAPI",
Version = version,
Description = $"Net5.WebAPI的 {version} 版本,可根据需要选择"
});
}); #region 为Swagger JSON and UI设置xml文档注释路径
string basePath = Path.GetDirectoryName(typeof(Program).Assembly.Location);//获取应用程序所在目录(绝对,不受工作目录影响,建议采用此方法获取路径)
string xmlPath = Path.Combine(basePath, "Net5.WebAPI.xml"); c.IncludeXmlComments(xmlPath);
#endregion // c.SwaggerDoc("v1", new OpenApiInfo { Title = "Net5.WebAPI", Version = "v1" });
});
} // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseSwagger();
app.UseSwaggerUI(c =>
{
typeof(ApiVersions).GetEnumNames().ToList().ForEach(version =>
{
c.SwaggerEndpoint($"/swagger/{version}/swagger.json", $"Net5.WebAPI {version}");
});
//c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "Net5.WebAPI v1")
});
} app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
添加版本号
在需要进行版本控制的控制器添加版本 [ApiExplorerSettings(GroupName = "V?")]
大功告成,运行效果如下
net5 webapi中 SwaggerUI如何进行版本控制的更多相关文章
- ASP.NET Core WebAPI中的分析工具MiniProfiler
介绍 作为一个开发人员,你知道如何分析自己开发的Api性能么? 在Visual Studio和Azure中, 我们可以使用Application Insight来监控项目.除此之外我们还可以使用一个免 ...
- ASP.NET Core 3.0 WebApi中使用Swagger生成API文档简介
参考地址,官网:https://docs.microsoft.com/zh-cn/aspnet/core/tutorials/getting-started-with-swashbuckle?view ...
- Autofac - MVC/WebApi中的应用
Autofac前面写了那么多篇, 其实就是为了今天这一篇, Autofac在MVC和WebApi中的应用. 一.目录结构 先看一下我的目录结构吧, 搭了个非常简单的架构, IOC(web), IBLL ...
- WebAPI中无法获取Session对象的解决办法
在MVC的WebApi中默认是没有开启Session会话支持的.需要在Global中重写Init方法来指定会话需要支持的类型 public override void Init() { PostAut ...
- webapi 中的本地登录
WebApi 身份验证方式 asp.net WebApi 中有三种身份验证方式 个人用户账户.用户可以在网站注册,也可以使用 google, facebook 等外部服务登录. 工作和学校账户.使用活 ...
- springboot中swaggerUI的使用
demo地址:demo-swagger-springboot springboot中swaggerUI的使用 1.pom文件中添加swagger依赖 2.从github项目中下载swaggerUI 然 ...
- webapi 中使用 protobuf
相比json来说,好处是速度更快,带宽占用更小.其效果大致等于json+Gzip. 在webapi中使用protobuf的方法为: 引用nuget包 Install-Package protobuf- ...
- 【AspNetCore】【WebApi】扩展Webapi中的RouteConstraint中,让DateTime类型,支持时间格式化(DateTimeFormat)
扩展Webapi中的RouteConstraint中,让DateTime类型,支持时间格式化(DateTimeFormat) 一.背景 大家在使用WebApi时,会用到DateTime为参数,类似于这 ...
- 在asp.net WebAPI 中 使用Forms认证和ModelValidata(模型验证)
一.Forms认证 1.在webapi项目中启用Forms认证 Why:为什么要在WebAPI中使用Forms认证?因为其它项目使用的是Forms认证. What:什么是Forms认证?它在WebAP ...
随机推荐
- Educational Codeforces Round 95 (Rated for Div. 2) B. Negative Prefixes (贪心,构造)
题意:给你一串长度为\(n\)的序列,有的位置被锁上了,你可以对没锁的位置上的元素任意排序,使得最后一个\(\le0\)的前缀和的位置最小,求重新排序后的序列. 题解:贪心,将所有能动的位置从大到小排 ...
- HTTP笔记1--Web及网络基础
web页面如何呈现? 客户端:通过发送请求获取服务器资源的 Web 浏览器 web是建立在 HTTP 协议上通信的 WWW(万维网/web)的构建技术 把 SGML(StandardGeneral ...
- 二进制安装kubernetes(五) kubelet组件安装
概述资料地址:https://blog.csdn.net/bbwangj/article/details/81904350 Kubelet组件运行在Node节点上,维持运行中的Pods以及提供kube ...
- when I was installing github for windows ,some errors occurred !
1: 2: 3: 4: install.log error messages:
- 如何强制删除 baidu/tempdata/con.dat 的垃圾文件! How to fix locked SD card: 读卡器 损坏,补救措施!
https://www.youtube.com/watch?v=y2c37dcxNto&feature=youtu.be 使用windows command prompt 强制删除 baidu ...
- Github access token
Github access token https://github.com/settings/tokens https://docs.github.com/en/free-pro-team@late ...
- Awesome GitHub Topics
Awesome GitHub Topics freeCodeCamp https://github.com/topics/javascript?o=desc&s=stars https://g ...
- GitHub & GraphQL API
GitHub & GraphQL API https://gist.github.com/xgqfrms/15559e7545f558d85c5efdea79171a3d refs xgqfr ...
- React Native & Android & Text Input
React Native & Android & Text Input react native clear input value https://stackoverflow.com ...
- Flutter: 获取本地json数据
FutureBuilder( future: DefaultAssetBundle.of(context).loadString('data/data.json'), builder: (contex ...