Why does this url resolve in 400 – Bad Request?
http://localhost:2785/api/ticker/Web.App.QuotesReaders/search=se%3Aabb
My environment is Visual Studio 2010, MVC 4 and the controller used is a WebApiController.
The %3A is an URL-encoded colon.
SOLUTION
This works for some reason:
http://localhost:2785/api/ticker?className=Web.App.QuotesReaders&search=se%3Aabb
… which means, I couldn’t specify this route in global.asax.cs:
/api/ticker/{className}/{search}
… nor this …
/api/ticker/{className}/search={search}
… but this …
/api/ticker
For further information: http://www.hanselman.com/blog/ExperimentsInWackinessAllowingPercentsAnglebracketsAndOtherNaughtyThingsInTheASPNETIISRequestURL.aspx
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
It seems that ASP.net does not allow colons before the ‘?’ in an URL, even if it is encoded as %3A.
For example, these won’t work
http://foo.org/api/persons/foo:bar
http://foo.org/api/persons/foo%3abar
But this works:
http://foo.org/api/persons?id=foo%3abar
In all examples, we would expect ASP.NET MVC to pass “foo:bar” as an id argument, properly decoded. I just tested this with MVC4 and it seems to work. It is annoying that it doesn’t accept the URL encoding before the question mark though, but I’m sure there is a good reason for it. Probably to keep everything before the question mark a valid URL and any arguments after the question mark.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0