GNaP.WebApi.Versioning
Usage
Install from NuGet in your Web API project: Install-Package GNaP.WebApi.Versioning
You can now use VersionedRoute
instead of Route
in your controllers to get API versioning for free on your routes.
Remember to turn on Attribute Based Routing with MapHttpAttributeRoutes() on your Web API configuration, in case it isn't already there (e.g.: Visual Studio 2012 Web API template does not contain it).
Example
Version 1 Controller
namespace GNaP.WebApi.Versioning.Example.Controllers
{
using System.Web.Http;
using GNaP.WebApi.Versioning;
[RoutePrefix("api/customers")]
public class CustomerVersion1Controller : ApiController
{
[VersionedRoute()]
public IHttpActionResult Get()
{
return Json(...);
}
[VersionedRoute("{id:int:min(1)}")]
public IHttpActionResult Get(int id)
{
return Json(...);
}
}
}
Version 2 Controller
namespace GNaP.WebApi.Versioning.Example.Controllers
{
using System.Web.Http;
using GNaP.WebApi.Versioning;
[RoutePrefix("api/customers")]
public class CustomerVersion2Controller : ApiController
{
[VersionedRoute(2)]
public IHttpActionResult Get()
{
return Json(...);
}
[VersionedRoute("{id:int:min(1)}", 2)]
public IHttpActionResult Get(int id)
{
return Json(...);
}
}
}
Supported Versioning Strategies
Query string
GET /api/customers/42?v=2
Api-version header
GET /api/customers/42
api-version: 2
Accept header
GET /api/customers/42
Accept: application/vnd.gnap.v2+json
NuGet
Copyright
Copyright © 2014-2015 Infrabel and contributors.
License
GNaP.WebApi.Versioning is licensed under BSD (3-Clause). Refer to LICENSE for more information.