c# - Versioning using Attribute Routing in ASP .net WEB API -
i trying implement versioning using attributerouting in web api. have defined 2 folders under controllers called v1 , v2. have multiple controllers in each folder. in product controller define
routeprefix [routeprefix("v1/product")] , [routeprefix("v2/product")]
when go uri v1/product works fine, v2/product executes code in v1 folder. attribute routing support versioning or have related routes well. route defined
config.routes.maphttproute( name: "defaultapi", routetemplate: "{namespace}/{controller}/{id}", defaults: new { id = routeparameter.optional} );
my product controller looks like
namespace api.controllers { [routeprefix("v1/product")] public class productv1controller : apicontroller { private dbcontext db = new dbcontext(); public dynamic get() { //gets products } }
the code in v2 product is
namespace api.controllers { [routeprefix("v2/product")] public class productv2controller : apicontroller { private dbcontext db = new dbcontext(); public dynamic get() { //gets products } }
can please suggest or provide link example implement versioning using attribute routing
you need decorate actions route attribute in order work.
[route] public dynamic get() ...
also, need have config.maphttpattributeroutes();
in webapiconfig's register method
update
here's link gist, tested in new web application webapi 5 , worked. https://gist.github.com/daviddesloovere/11367286
Comments
Post a Comment