API服务版本控制 Microsoft.AspNetCore.Mvc.Versioning
我们在进行webapi服务开发时,会遇到一些多个版本的api共存的情况发生,例如某一版本APP上线后,需求发生变更,需要在下一个升级版本更新API,但同时又需要保证这个APP版本能正常使用,这时候就需要采用API服务版本控制。
版本控制一般有以下几种方式:
- 在url上增加查询字符串参数的方式,追加版本,例如 api/service?v=2
- 在url路径上增加版本。例如:api/v2/service(这种方式个人认为目前是最优雅的方式)
- 在http请求head中加入版本标识
目前微软提供了 Microsoft.AspNetCore.Mvc.Versioning 组件,能够支持以上几种方式。
组件注册
services.AddApiVersioning(options =>
{
options.ReportApiVersions = true;
options.AssumeDefaultVersionWhenUnspecified = true;
options.DefaultApiVersion = new ApiVersion(, );
});
- ReportAPIVersions: 这是可选的。但是, 当设置为 true 时, API 将返回响应标头中支持的版本信息。
- AssumeDefaultVersionWhenUnspecified: 此选项将用于不提供版本的请求。默认情况下, 假定的 API 版本为1.0。(这个经测试对url追加版本方式不起作用)
- DefaultApiVersion: 此选项用于指定在请求中未指定版本时要使用的默认 API 版本。这将默认版本为1.0。
几个重要的类
- ApiVersion 特性标识当前Controller的版本,注意路由的配置。这里我们采用了第 2 个版本控制方式
- MapToApiVersion 属性允许将单个 API 操作映射到某一版本
实现一个小需求:在同一个Controller里面新增一个api版本
http://localhost:5000/gateway/api/user/headclaims --->指向 默认v1版本
http://localhost:5000/gateway/api/v1/user/headclaims ---->指向 v1版本
http://localhost:5000/gateway/api/v2/user/headclaims ---->指向 v2版本
配置如下:
[ApiVersion("2.0")]
//多路由支持
[Route("api/v{version:apiVersion}/[controller]")]
[Route("api/[controller]")]
[ApiController]
public class UserController : ControllerBase
{
private readonly IUserAppService _userService;
{
_userService = userService;
}
[HttpGet]
public IActionResult HeadClaims()
{
var claimTypes = new List<string> { "name", "phone", "userId", "introduce" };
var claimHeads = Request.Headers.Where(x => claimTypes.Contains(x.Key));
var user = Request.HttpContext.User;
if (claimHeads == null) return Ok();
var returnObj = new JObject();
foreach (var ch in claimHeads)
{
returnObj.Add(ch.Key, ch.Value.ToString());
}
return Ok(returnObj);
}
[HttpGet, MapToApiVersion("2.0")]
public IActionResult HeadClaimsv2()
{
var claimTypes = new List<string> { "name", "phone" };
var claimHeads = Request.Headers.Where(x => claimTypes.Contains(x.Key));
var user = Request.HttpContext.User;
if (claimHeads == null) return Ok();
var returnObj = new JObject();
foreach (var ch in claimHeads)
{
returnObj.Add(ch.Key, ch.Value.ToString());
}
return Ok(returnObj);
}
API服务版本控制 Microsoft.AspNetCore.Mvc.Versioning的更多相关文章
- Orchard Core 版本冲突 The type 'FormTagHelper' exists in both 'Microsoft.AspNetCore.Mvc.TagHelpers, Version=2.1.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' and...
最近老大让我看Orchard Core,这是一个CMS系统.可以先参考大佬的文章:https://www.cnblogs.com/shanyou/archive/2018/09/25/9700422. ...
- TypeLoadException: Could not load type 'Microsoft.AspNetCore.Mvc.Internal.IHttpResponseStreamWriterFactory' from assembly 'Microsoft.AspNetCore.Mvc.Core, Version=2.1.2.0 ...
今天调试 asp.net core 2.0 项目时遇到了如下错误: TypeLoadException: Could not load type 'Microsoft.AspNetCore.Mvc.I ...
- Core 3.1 MVC 抛异常“InvalidOperationException: No service for type 'Microsoft.AspNetCore.Mvc.ViewFeatures.ITempDataDictionaryFactory' has been registered.”
.NET Core 的版本是 3.1遇到的问题是 Action 中 return View() 的时候报错 An unhandled exception occurred while processi ...
- .Net Core Api 使用版本控制
1,安装Microsoft.AspNetCore.Mvc.Versioning NET Core Mvc中,微软官方提供了一个可用的Api版本控制库Microsoft.AspNetCore.Mvc.V ...
- 002.Create a web API with ASP.NET Core MVC and Visual Studio for Windows -- 【在windows上用vs与asp.net core mvc 创建一个 web api 程序】
Create a web API with ASP.NET Core MVC and Visual Studio for Windows 在windows上用vs与asp.net core mvc 创 ...
- 从ASP.Net Core Web Api模板中移除MVC Razor依赖项
前言 :本篇文章,我将会介绍如何在不包括MVC / Razor功能和包的情况下,添加最少的依赖项到ASP.NET Core Web API项目中. 一.MVC VS WebApi (1)在ASP. ...
- 使用Microsoft.AspNetCore.TestHost进行完整的功能测试
简介 Microsoft.AspNetCore.TestHost是可以用于Asp.net Core 的功能测试工具.很多时候我们一个接口写好了,单元测试什么的也都ok了,需要完整调试一下,检查下单元测 ...
- 错误记录——fail: Microsoft.AspNetCore.Server.Kestrel[13]
fail: Microsoft.AspNetCore.Server.Kestrel[13] Connection id "0HLPN4417RVEM", Request id &q ...
- Web API 接口版本控制 SDammann.WebApi.Versioning
前言 在设计对外 Web API 时,实务上可能会有新旧版本 API 并存的情况,例如开放 Web API 给厂商串接,但同一个服务更新版本时,不一定所有厂商可以在同一时间都跟着更新他们的系统,但如果 ...
随机推荐
- Ubuntu 12.04 编译bcm93349dcm软件包
1.准备工作操作系统:Ubuntu 12.04 获取bcm93349dcm软件包: bootloader源代码:Bootloader_2_2_0.zip CM源代码:ProdD20_BFC4.4.10 ...
- CentOS-TFTP服务搭建
title date tags layout CentOS6.5 TFTP搭建 2018-08-26 Centos6.5服务器搭建 post 1.安装TFTP服务 yum install tftp-s ...
- SMTP错误码/建议解决方法
SMTP错误码/建议解决方法 错误总表 420 1. Timeout Communication Problem Encountered During Transmission. Thie Is a ...
- ORs-5-OR Subgenomes Variation among Birds, Sea Turtle and Alligator
OR Subgenomes Variation among Birds, Sea Turtle and Alligator 由 该图数据计算每种鸟的relative percentage,得到下图: ...
- HTML的img标签:alt属性和title属性
当浏览器卖主扭曲了标准并且自顾自的不按规则去做一些事,他们可能会造成一些问题,或者至少产生了混淆.例子之一就是一些浏览器处理alt属性(一般会被错误的称作alt标签)的方式,比如拥有大量用户的Wind ...
- 数位dp——BZOJ1026 Windy数
1026: [SCOI2009]windy数 Time Limit: 1 Sec Memory Limit: 162 MB Description windy定义了一种windy数.不含前导零且相邻 ...
- jQuery中的bind(), live(), on(), delegate()
当我们试图绑定一些事件到DOM元素上的时候,我相信上面这4个方法是最常用的.而它们之间到底有什么不同呢?在什么场合下用什么方法是最有效的呢? 准备知识: 当我们在开始的时候,有些知识是必须具备的: D ...
- Vue错误信息解决
在运行Vue项目时提示如下错误: [Vue warn]: You are using the runtime-only build of Vue where the template compiler ...
- tomcat6版本虚拟目录详细配置
在tomcat6版本中: 一. 1.[官方文档]本人不推荐. 在tomcat\conf下server.xml中找到 <Host name="localhost" appBa ...
- 重启aliyun esc 需要重新启动redis
/export/sorftware ./redis-server ../redis.conf redis-server 配置路径 如redis-server /etc/redis.conf 不是后 ...