c# - ASP.net Web Api Versioning -
i have asp.net web api project , decided time support versioning. using official microsoft nuget support versioning (more info here), , decided version namespace (as exampled here).
unfortunately cannot code work. if call method this:
http://localhost:7291/api/saved/getnumberofsavedworkoutsforuser?api-version=2.0
i error:
multiple types found match controller named 'saved'. can happen if route services request ('api/{controller}/{action}/{id}') found multiple controllers defined same name differing namespaces, not supported.
and if call this: http://localhost:7291/v2/saved/getnumberofsavedworkoutsforuser
i error 404:
the resource looking has been removed, had name changed, or temporarily unavailable.
i not sure doing wrong. here code:
startup.cs
public void configuration(iappbuilder app) { var configuration = new httpconfiguration(); var httpserver = new httpserver(configuration); app.usecors(microsoft.owin.cors.corsoptions.allowall); // reporting api versions return headers "api-supported-versions" , "api-deprecated-versions" configuration.addapiversioning(o => { o.assumedefaultversionwhenunspecified = true; o.reportapiversions = true; o.defaultapiversion = apiversion.default; }); configuration.routes.maphttproute( "versionedurl", "v{apiversion}/{controller}/{action}/{id}", defaults: null, constraints: new { apiversion = new apiversionrouteconstraint() }); configuration.routes.maphttproute( "versionedquerystring", "api/{controller}/{action}/{id}", defaults: null); app.usewebapi(httpserver); configureauth(app); }
saved controller (v1)
namespace master.infrastructure.api.controllers { [authorize] [routeprefix("api/saved")] [apiversion("1.0")] public class savedcontroller : apicontroller { private readonly iuserservice _userservice; public savedcontroller(iuserservice userservice) { _userservice = userservice; } [httpget] [actionname("getnumberofsavedworkouts")] public async task<numberofsavedworkouts> getnumberofsavedworkouts() { var numofsavedworkouts = new numberofsavedworkouts { currentnumberofsavedworkouts = await _userservice.getnumberofsavedworkoutsforuserasync(user.identity.getuserid()) }; return numofsavedworkouts; } } }
saved controller (v2)
namespace master.infrastructure.api.v2.controllers { [authorize] [apiversion("2.0")] [routeprefix("v{version:apiversion}/saved")] public class savedcontroller : apicontroller { private readonly isavedworkoutservice _savedworkoutservice; public savedcontroller(isavedworkoutservice savedworkoutservice) { _savedworkoutservice = savedworkoutservice; } [actionname("getnumberofsavedworkoutsforuser")] public async task<ihttpactionresult> getnumberofsavedworkoutsforuser() { var cnt = await _savedworkoutservice.countnumberofsavedworkoutsforuser(user.identity.getuserid()); return ok(cnt); } } }
Comments
Post a Comment