URL based versioning in ASP.NET Core

URL based Versioning

In this type of versioning, we can define versions in a URL so that it is more readable. Most users prefer this type over other types. You can to URL based versioning by changing the routs as [Route("api/{v:apiVersion}/Values")]. Let’s modify the Route attribute in our both controller, as follows.

  using Microsoft.AspNetCore.Mvc;
  namespace api_versioning_demo.Controllers
  {
	  [ApiController]
	  [ApiVersion("1.0")]
	  [Route("api/{v:apiVersion}/employee")]
	  public class EmployeeV1Controller : ControllerBase
	  {
		  [HttpGet]
		  public IActionResult Get()
		  {
			  return new OkObjectResult("employees from v1 controller");
		  }
	  }
  }
  using Microsoft.AspNetCore.Mvc;
  namespace api_versioning_demo.Controllers
  {
	  [ApiController]
	  [ApiVersion("2.0")]
	  [Route("api/{v:apiVersion}/employee")]
	  public class EmployeeV2Controller : ControllerBase
	  {
		  [HttpGet]
		  public IActionResult Get()
		  {
			  return new OkObjectResult("employees from v2 controller");
		  }
	  }
  }

To call EmployeeV1Controller, we have to hit as https://localhost:44381/api/1.0/employee

To call EmployeeV2Controller we need: https://localhost:44381/api/2.0/employee (View Highlight)


Additional Metadata