An elegant way to version API
Does not use versioning through the route, but by the header of the http protocol
- .Net 5
- Web API
- Microsoft.AspNetCore.Mvc.Versioning
- Run API as Kestrel
- dotnet run
- Application open port 5000
- Use Postman to see informations of API
Versions
- 1.0 - Deprecated
- 2.0 - Supported
Headers Version
- version
- api-version
- x-version
It is possible to apply versions in the same file,
Not necessary to create multiple folders with the same controller.
[HttpGet]
[Route("Products")]
[MapToApiVersion("1.0")]
public IEnumerable<Product> GetProductsFullPrice()
{
var random = new Random();
var price = random.NextDouble();
return Enumerable.Range(1, 3).Select(index =>
new Product
{
Name = Product.Types[random.Next(Product.Types.Length)],
FullPrice = price
})
.ToArray();
}
[HttpGet]
[Route("Products")]
[MapToApiVersion("2.0")]
public IEnumerable<Product> GetProductsReducedPrice()
{
var random = new Random();
var price = random.NextDouble();
return Enumerable.Range(1, 3).Select(index =>
new Product
{
Name = Product.Types[random.Next(Product.Types.Length)],
FullPrice = price,
ReducedPrice = price * 0.1
})
.ToArray();
}
}