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?