创建项目

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. Complete the sequence! POJ - 1398 差分方法找数列规律

    参考链接:http://rchardx.is-programmer.com/posts/16142.html vj题目链接:https://vjudge.net/contest/273000#stat ...

  2. codeforces 10C Digital Root(非原创)

    Not long ago Billy came across such a problem, where there were given three natural numbers A, B and ...

  3. Vue 组件之间通信 All in One

    Vue 组件之间通信 All in One 组件间通信 1. 父子组件之间通信 https://stackblitz.com/edit/vue-parent-child-commutation?fil ...

  4. AST & js interpreter

    AST & js interpreter 抽象语法树 & Javascript 解析器 https://astexplorer.net/ https://esprima.org/dem ...

  5. taro taro 多端同步调试

    taro 多端同步调试 debug https://nervjs.github.io/taro/docs/envs-debug.html

  6. django学习-27.admin管理后台里:对列表展示页面的数据展示进行相关优化

    目录结构 1.前言 2.完整的操作步骤 2.1.第一步:查看ModelAdmin类和BaseModelAdmin类的源码 2.2.第二步:查看表animal对应的列表展示页面默认的数据展示 2.3.第 ...

  7. [转]【视觉 SLAM-2】 视觉SLAM- ORB 源码详解 2

    转载地址:https://blog.csdn.net/kyjl888/article/details/72942209 1 ORB-SLAM2源码详解 by 吴博 2 https://github.c ...

  8. IO多路复用之select、poll、epoll

    本文转载自IO多路复用之select.poll.epoll 导语 IO多路复用:通过一种机制,一个进程可以监视多个描述符,一旦某个描述符就绪(一般是读就绪或者写就绪),能够通知程序进行相应的读写操作. ...

  9. C++单链表反转、两有序链表合并仍有序

    1 #include<iostream> 2 3 struct Node 4 { 5 int data; 6 Node *next; 7 }; 8 9 typedef struct Nod ...

  10. [Python] 基于 jieba 的中文分词总结

    目录 模块安装 开源代码 基本用法 启用Paddle 词性标注 调整词典 智能识别新词 搜索引擎模式分词 使用自定义词典 关键词提取 停用词过滤 模块安装 pip install jieba jieb ...