asp.net api core 添加应用版本切换

主要参考: 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

 

posted @ 2018-10-08 18:35  游子善心  阅读(5)  评论(0编辑  收藏  举报  来源