c# - Web Api Get() route not working when return type is HttpResponseMessage -
this causing me bit of headache. ultimately, i'm trying homecoming image i've been experimenting , simplified using string.
what want: go url:
http://xxx/api/helloworld/1
responds with: "hello world!"
the next web api declarations work url supplied;
public string get([fromuri]int id) { homecoming "hello world"; } public task<string> get([fromuri]int id) { homecoming "hello world"; }
what not work;
public httpresponsemessage get([fromuri]int id) { homecoming request.createresponse<string>(httpstatuscode.ok, "hello world"); } public httpresponsemessage get([fromuri]int id) { httpresponsemessage response = new httpresponsemessage(); string text = "hello world"; memorystream test = new memorystream(); test.write(conversionutilities.tobytes(text) /*custom string->byte[] method, utf-8 encoding*/, 0, text.length); response.content = new streamcontent(test); response.content.headers.contenttype = new mediatypeheadervalue("text/plain"); response.statuscode = httpstatuscode.ok; homecoming response; }
what happens when have get() homecoming type of httpresponsemessage next error returned;
no http resource found matches request uri "http://xxx/api/helloworld/1"
this error appears particular homecoming type. now, routing in webapiconfig.cs file follows (and works "string" homecoming types);
// controller id // handle routes "/api/vtrouting/1" config.routes.maphttproute( name: "controllerandid", routetemplate: "api/{controller}/{id}", defaults: null, constraints: new { id = @"^\d+$" } // integers ); // controllers actions // handle routes "/api/vtrouting/route" config.routes.maphttproute( name: "controllerandaction", routetemplate: "api/{controller}/{action}" ); // controller // handle routes "/api/vtrouting" config.routes.maphttproute( name: "controlleronly", routetemplate: "api/{controller}" );
any thoughts? i'm stumped homecoming type behaviour :-s
found cause! had created blank api controller per template worked. cue bit of copy/pasting narrow downwards cause. it's subtle , code posted worked because changed variable names posting publicly - cause of issue. go figure.
to replicate, create template per normal. create method;
public string get(int id)
change to;
public string get(int personid)
and seek run. you'll error described above. seems declaration of get/post/etc parameters must match specified in routing. if alter parameter "personid" did, can prepare either renaming parameter default "id" or modifying routing updating name;
config.routes.maphttproute( name: "controllerandid", routetemplate: "api/{controller}/{personid}", defaults: null, constraints: new { personid = @"^\d+$" } // integers );
note "{personid}" in routetemplate , constraint parameters. name of field must match of name of parameter. never says in doc when looking how download files or looking @ web api. when going high level of detail on routing/mvc have trips n00b not familiar either. mind you, more experienced collegues didn't spot either :-). hope helps others same pain!
c# asp.net-mvc asp.net-web-api asp.net-mvc-routing
No comments:
Post a Comment