32
loading...
This website collects cookies to deliver better user experience
Install-Package Microsoft.AspNetCore.Mvc.Versioning
startup.cs
, put the following code:
services.AddApiVersioning(x =>
{
x.DefaultApiVersion = new ApiVersion(1, 0);
x.AssumeDefaultVersionWhenUnspecified = true;
x.ReportApiVersions = true;
x.ApiVersionReader = new HeaderApiVersionReader("x-api-version");
});
x.DefaultApiVersion
-> indicates the default version of the API.x.AssumeDefaultVersionWhenUnspecified
-> indicates if the default version of the API will be used if in the request header we don't indicate the version of the API.x.ReportApiVersions
-> indicates if we want to show in the response the available versions of the API.x.ApiVersionReader
-> indicates how we are going to send the API version in the request.[ApiVersion("X.0")]
, where X.0
will be the version number of your API for this class. In case your controller does not have this decorator, the API version for this class will be the default version (in our case "1.0").[Route("api/[controller]")]
[ApiController]
public class DemoController : ControllerBase
{
[HttpGet]
public IActionResult GetDemo()
{
return Content("Hola desde la versión por defecto");
}
}
[Route("api/[controller]")]
[ApiController]
[ApiVersion("2.0")]
public class DemoController : ControllerBase
{
[HttpGet]
public IActionResult GetDemo()
{
return Content("Hola desde la versión 2");
}
}
ReportApiVersions = true
in the configuration.32