主要参考: https://github.com/Microsoft/aspnet-api-versioning
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddOptions();
// 添加程序版本控制
services.AddApiVersioning();
}
ApiController.cs
[ApiVersion("1.0")]
[ApiVersion("2.0")]
[Route("api/v{version:apiVersion}/[controller]")]
[ApiController]
public class TestController : BaseController
{
[HttpGet,MapToApiVersion("1.0")]
public IActionResult Get1()
{
return Ok("v1.0");
}
[HttpGet, MapToApiVersion("2.0")]
public IActionResult Get2()
{
return Ok("v2.0");
}
}
Result
xxx.com/api/v1.0/Test // v1.0
xxx.com/api/v2.0/Test // v2.0