c# - ASP.net WebApi newly registered route says, that request is invalid -
c# - ASP.net WebApi newly registered route says, that request is invalid -
i'm setting backend windows phone 8.1 app. i'm using asp.net webapi create restful api accessing info db, set on windows azure.
this how routes looks:
// web api configuration , services // web api routes config.maphttpattributeroutes(); config.routes.maphttproute( name: "defaultapi", routetemplate: "api/{controller}/{id}", defaults: new { id = routeparameter.optional } ); config.routes.maphttproute( name: "defaultnamedapi", routetemplate: "api/{controller}/{name}", defaults: new { name = routeparameter.optional } );
what i'm trying accomplish accesing info using not integer - want access info using string. code webapi controller:
private smokopediacontext db = new smokopediacontext(); // api/images public iqueryable<imagemodel> getimagemodels() { homecoming db.imagemodels; } // api/images/5 [responsetype(typeof(imagemodel))] public ihttpactionresult getimagemodel(int id) { imagemodel imagemodel = db.imagemodels.find(id); if (imagemodel == null) { homecoming notfound(); } homecoming ok(imagemodel); } public ihttpactionresult getimagemodel(string name) { imagemodel imagemodel = db.imagemodels.find(name); if(imagemodel == null) { homecoming notfound(); } homecoming ok(imagemodel); } protected override void dispose(bool disposing) { if (disposing) { db.dispose(); } base.dispose(disposing); } private bool imagemodelexists(int id) { homecoming db.imagemodels.count(e => e.id == id) > 0; }
most of import code overload getimagemodel string parameter.
server returning error says parameter of url incorrect:
<error><message>the request invalid.</message><messagedetail>the parameters dictionary contains null entry parameter 'id' of non-nullable type 'system.int32' method 'system.web.http.ihttpactionresult getimagemodel(int32)' in 'smokopedia.controllers.dragoncontroller'. optional parameter must reference type, nullable type, or declared optional parameter.</messagedetail></error>
what should right in route?
there no difference in uri template terms between api/{controller}/{id}
, api/{controller}/{name}
; arbitrary names assign parameters can't used in resolving route.
take @ overload web api action method based on parameter type illustration of how "overload" routes based on parameter types.
c# asp.net rest azure asp.net-web-api
Comments
Post a Comment