Skip to content

vldmatos/API-Versions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

API-Versions

An elegant way to version API
Does not use versioning through the route, but by the header of the http protocol

Using

  • .Net 5
  • Web API
  • Microsoft.AspNetCore.Mvc.Versioning

How to use

  • Run API as Kestrel
  • dotnet run
  • Application open port 5000
  • Use Postman to see informations of API

Informations

  • Versions

    • 1.0 - Deprecated
    • 2.0 - Supported
  • Headers Version

    • version
    • api-version
    • x-version

Different version on the same controller

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();
	}
}

Result on Postman:

result

About

Applying an elegant form of versioning

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages