c# - Works for GET, not for POST (404 error) -
i have webapi controller servicesapicontroller
in mvc4 project signature so:
[authorize(roles = "sysadmin,secretary")] [acceptverbs("get", "post")] public ienumerable<stock> toprankedstocks(int maxrecords) { // stuff return result; }
my routing api controllers looks this:
config.routes.maphttproute( name: "servicesapi", routetemplate: "api/{controller}/{action}", defaults: new { controller = "servicesapi", action="index" } );
using iis express, if navigate http://localhost:62281/api/servicesapi/toprankedstocks?maxrecords=20
in browser, nice xml list of data objects.
however, if call so:
myhttpclient.baseaddress = "http://localhost:62281/"; var arguments = new list<keyvaluepair<string, string>>(new[] { keyvaluepair<string, string>("maxrecords", "10") }); var content = new formurlencodedcontent(arguments); var result = myhttpclient.postasjsonasync("/api/servicesapi/toprankedstocks", content).result;
...i 404 (not found) error.
post operations work in other places in code. config files application , iis express allow verbs. have no idea why not working. can clue me in?
i don't know if of relevant information, i'll post in case there's value here:
- windows 8 64 w/ 4gb ram
- visual studio 2012 professional update 2 resharper
- iis express 8
- .net framework 4.5
i can see potential issue controller method:
the problem argument of type int
(primitive type) - long don't mark [frombody]
webapi parameter binding engine assume comes url. explains why works; i'm not sure how post considered when don't supply value in url.
still, if marked maxrecords frombody , tried using method stop working probably. also, note in case wanted use form-urlencoded
string post body 1 key-value-pair make work you'd need post "=10"
, not "maxrecords=10"
.
to check if issue that's causing method fail, i'd temporarily remove support leaving post, , mark param frombody.
Comments
Post a Comment