webapi versioning the easy way
Turn out that doing webapi service versioning is relatively easy in Asp.net core. First off, please install the following nuget package :-
Install-Package Microsoft.AspNetCore.Mvc.Versioning -Version 2.0.0
Next up, is to organize your controllers into folders. I prefer to use folder (namespace) as a way to do it, so i can have the same controller name versioned differently, as shown below :-
--->V1-->Controller-->HelloController(version 1)
-->V2 -->Controller-->HelloController (version 2)
Next we just need to decorate our HelloController with the proper attribute.
Version 1 Controller.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ApiVersion("1.0")] | |
[Route("v{version:apiVersion}/[controller]")] | |
public class HelloController : Controller | |
{ | |
private AppConfig _settings; | |
private readonly ILogger<HelloController> _logger; | |
private const string HelloWorldMessage = "Hello World. V3"; | |
public HelloController(IOptions<AppConfig> appSettings, ILogger<HelloController> logger) | |
{ | |
_settings = appSettings?.Value; | |
_logger = logger; | |
} | |
// GET hello/index | |
[HttpGet("{id}")] | |
public IActionResult Index() | |
{ | |
var messageToUsers = HelloWorldMessage + DateTime.Now.ToShortTimeString(); | |
_logger.LogWarning(JsonConvert.ToString(messageToUsers)); | |
return Ok(messageToUsers); | |
} | |
// PUT api/values/5 | |
[HttpPut("{id}")] | |
public void Put(int id, [FromBody]string value) | |
{ | |
} | |
// DELETE api/values/5 | |
[HttpDelete("{id}")] | |
public void Delete(int id) | |
{ | |
} | |
[HttpPost] | |
public void Post([FromBody]string value) | |
{ | |
} | |
} |
Version 2 Controller
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ApiVersion("2.0")] | |
[Route("v{version:apiVersion}/[controller]")] | |
public class HelloController : Controller | |
{ | |
private AppConfig _settings; | |
private readonly ILogger<HelloController> _logger; | |
private const string HelloWorldMessage = "Hello World. V3"; | |
public HelloController(IOptions<AppConfig> appSettings, ILogger<HelloController> logger) | |
{ | |
_settings = appSettings?.Value; | |
_logger = logger; | |
} | |
// GET hello/index | |
[HttpGet("{id}")] | |
public IActionResult Index() | |
{ | |
var messageToUsers = HelloWorldMessage + DateTime.Now.ToShortTimeString(); | |
_logger.LogWarning(JsonConvert.ToString(messageToUsers)); | |
return Ok(messageToUsers); | |
} | |
// PUT api/values/5 | |
[HttpPut("{id}")] | |
public void Put(int id, [FromBody]string value) | |
{ | |
} | |
// DELETE api/values/5 | |
[HttpDelete("{id}")] | |
public void Delete(int id) | |
{ | |
} | |
[HttpPost] | |
public void Post([FromBody]string value) | |
{ | |
} | |
} |
Comments