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).
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(...);
}
}
}
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(...);
}
}
}
GET /api/customers/42?v=2
GET /api/customers/42
api-version: 2
GET /api/customers/42
Accept: application/vnd.gnap.v2+json
Copyright © 2014-2015 Infrabel and contributors.
GNaP.WebApi.Versioning is licensed under BSD (3-Clause). Refer to LICENSE for more information.