最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Versioning an API, not working as expected - Stack Overflow

programmeradmin1浏览0评论

I'm versioning an API and as far as I can tell, I've carried out all the instructions but can't seem to get it to work as intended.

program.cs:

builder.Services.AddApiVersioning(option =>
{
    option.AssumeDefaultVersionWhenUnspecified = true;
    option.DefaultApiVersion = new ApiVersion(1, 0); 
    option.ReportApiVersions = true;
    option.ApiVersionReader = ApiVersionReader
            .Combine(new HeaderApiVersionReader("X-Version"));
}).AddApiExplorer(options => {
    options.GroupNameFormat = "'v'VVV";
    options.SubstituteApiVersionInUrl = true; 
});

API Controller

[Route("api/[controller]")]
[ApiVersion(1.0)]
[ApiVersion(2.0)]
[ApiController]
public class TestController : ControllerBase
{
    [HttpGet]
    [MapToApiVersion(1.0)]
    [Produces("application/vnd.json")]
    public IActionResult GetTest()
    {
            return Ok("Test 1");
    }
 
    [HttpGet]
    [MapToApiVersion(2.0)]
    [Produces("application/vnd.json")]
    public IActionResult GetTestTwo()
    {
        return Ok("Test 2");
    }
}

In Postman, I add a Header under the header tab, X-Version with the value 1.0 and then 2.0 but every time I call the API, it always returns Test 2. I'm not sure what I may have missed in this simple example?

I've gone over some YT tutorials but they reference the previous versioning package however the new one seems to be equivalent but still can tell what I may have missed?

发布评论

评论列表(0)

  1. 暂无评论