I'm implementing Api Versioning in my NetCore 3 project using package Microsoft.AspNetCore.Mvc.Versioning
but it's throwing me an error if I try to add version into my router - it works fine if I don't pass the route.
Here is what I have done:
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
namespace Aus.GGG.Company
{
[Route("v{version:apiVersion}/[controller]")]
[ApiController]
[ApiVersion("1.0")]
public class BaseController : ControllerBase
{
[HttpGet("/test.json")]
public async Task<IActionResult> Get() => Ok("test");
}
}
That is how I have added my service:
public static IServiceCollection RegisterExtraStuff(this IServiceCollection services)
{
services.AddApiVersioning(config =>
{
config.DefaultApiVersion = new ApiVersion(1, 0);
config.AssumeDefaultVersionWhenUnspecified = true;
config.ReportApiVersions = true;
});
return services;
}
}
And here is the curl that doesn't works:
curl --location --request GET 'http://localhost:3000/v1.0/test.json
And the curl that works:
curl --location --request GET 'http://localhost:3000/test.json
In the last curl, I can see the extra header api-supported-versions
on response so my configuration looks right, I guess.
Any idea on what is missing?
You add a slash at the begining in the action route, it will ignore the route attribute on the controller.
If you want the format like this:
You can change it like below: