创建项目

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如何进行版本控制的更多相关文章

  1. ASP.NET Core WebAPI中的分析工具MiniProfiler

    介绍 作为一个开发人员,你知道如何分析自己开发的Api性能么? 在Visual Studio和Azure中, 我们可以使用Application Insight来监控项目.除此之外我们还可以使用一个免 ...

  2. ASP.NET Core 3.0 WebApi中使用Swagger生成API文档简介

    参考地址,官网:https://docs.microsoft.com/zh-cn/aspnet/core/tutorials/getting-started-with-swashbuckle?view ...

  3. Autofac - MVC/WebApi中的应用

    Autofac前面写了那么多篇, 其实就是为了今天这一篇, Autofac在MVC和WebApi中的应用. 一.目录结构 先看一下我的目录结构吧, 搭了个非常简单的架构, IOC(web), IBLL ...

  4. WebAPI中无法获取Session对象的解决办法

    在MVC的WebApi中默认是没有开启Session会话支持的.需要在Global中重写Init方法来指定会话需要支持的类型 public override void Init() { PostAut ...

  5. webapi 中的本地登录

    WebApi 身份验证方式 asp.net WebApi 中有三种身份验证方式 个人用户账户.用户可以在网站注册,也可以使用 google, facebook 等外部服务登录. 工作和学校账户.使用活 ...

  6. springboot中swaggerUI的使用

    demo地址:demo-swagger-springboot springboot中swaggerUI的使用 1.pom文件中添加swagger依赖 2.从github项目中下载swaggerUI 然 ...

  7. webapi 中使用 protobuf

    相比json来说,好处是速度更快,带宽占用更小.其效果大致等于json+Gzip. 在webapi中使用protobuf的方法为: 引用nuget包 Install-Package protobuf- ...

  8. 【AspNetCore】【WebApi】扩展Webapi中的RouteConstraint中,让DateTime类型,支持时间格式化(DateTimeFormat)

    扩展Webapi中的RouteConstraint中,让DateTime类型,支持时间格式化(DateTimeFormat) 一.背景 大家在使用WebApi时,会用到DateTime为参数,类似于这 ...

  9. 在asp.net WebAPI 中 使用Forms认证和ModelValidata(模型验证)

    一.Forms认证 1.在webapi项目中启用Forms认证 Why:为什么要在WebAPI中使用Forms认证?因为其它项目使用的是Forms认证. What:什么是Forms认证?它在WebAP ...

随机推荐

  1. Educational Codeforces Round 97 (Rated for Div. 2) C. Chef Monocarp (DP)

    题意:有\(n\)个菜在烤箱中,每个时刻只能将一个菜从烤箱中拿出来,第\(i\)个时刻拿出来的贡献是\(|i-a[i]|\),你可以在任意时刻把菜拿出来,问将所有菜拿出的最小贡献是多少? 题解: 先对 ...

  2. Codeforces Round #649 (Div. 2)

    Codeforces Round #649 (Div. 2) -- WKL \(\mathcal{A}\)题: \(\mathrm{XXXXX}\) Greedy implementation *12 ...

  3. 牛客编程巅峰赛S1第5场 - 青铜&白银 C.排队 (优先队列,归并排序)

    题意:有\(m\)个窗口,\(n\)个人排队,每个人都有各自的办理时间,只有办理完成窗口才能空出来,后面的人开始办理,求有多少人比后面的人开始办理的早但完成的晚. 题解:我们可以用优先队列来模拟办理, ...

  4. Python进阶丨如何创建你的第一个Python元类?

    摘要:通过本文,将深入讨论Python元类,其属性,如何以及何时在Python中使用元类. Python元类设置类的行为和规则.元类有助于修改类的实例,并且相当复杂,是Python编程的高级功能之一. ...

  5. Linux-文件查看命令

    目录 系统文件查看命令-cat 系统文件查看命令-more 系统文件查看命令-less 系统文件查看命令-head 系统文件查看命令-tail 系统文件查看命令-grep 文件上传下载命令-rz,sz ...

  6. 网络安全知识--PHP代码审计/Web For Pantesters 的 XSS

    用到 ** WEB FOR Pentester** 注意区分单引号双引号. 常见代码 审计工具 wamp,dwva,zvuldrill,burpsuite,seay源代码审计系统... 1 xss W ...

  7. Tomcat连接配置

    DBCP连接池配置: <bean class="org.apache.tomcat.jdbc.pool.PoolProperties"> <property na ...

  8. SMB relay

    SMB relay 0x00 SMB服务 先来了解一下什么是 SMB 服务,SMB(Server Message Block)是一个协议名称,用它可以共享计算机之间的文件.打印机.串口等,通过 SMB ...

  9. 基于HSV彩色空间与直方图信息的植物叶脉FFCM算法提取

    宣旭峰,王美丽,张建锋. 基于HSV彩色空间与直方图信息的植物叶脉FFCM算法提取[J/OL]. 计算机应用研究,2018,(08):1-7. (2017-07-21)[2017-10-09]. ht ...

  10. Apple 反人类的设计的产品组合

    Apple 反人类的设计的产品组合 Apple shit design macbook pro 2018 + beats solo3 MBP 的耳机孔在电脑右边, betas 的耳机孔在左边, 组合起 ...