7

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

  • Did you mean to write "?search=s3%3Aabb" ? – McGarnagle Apr 08 '12 at 20:27
  • Bridget: http://stackoverflow.com/questions/2593156/how-can-i-send-a-get-request-containing-a-colon-to-an-asp-net-mvc2-controller/2593364#2593364 – Cheeso Apr 08 '12 at 20:27
  • @dbaseman No, I did not not mean to write that. – Bridget the Midget Apr 08 '12 at 20:33
  • @BridgettheMidget, I guess you assume we all know this `Web.App.QuotesReaders` application and know what it does, how it does – L.B Apr 08 '12 at 20:43
  • @L.B You don't need to. The url works without the colon. – Bridget the Midget Apr 08 '12 at 20:48
  • @BridgettheMidget As I see in your solution , problem is still related with how to call your ticker api not with colons etc. So How do you expect us to know what `ticker` is – L.B Apr 08 '12 at 20:52
  • @L.B Given that the URL works without the colon, I don't see how the call to my webapi itself can be a problem. But... ticker is the action and api is the controller. The rest are parameters. Read this for further information on the problem: http://www.hanselman.com/blog/ExperimentsInWackinessAllowingPercentsAnglebracketsAndOtherNaughtyThingsInTheASPNETIISRequestURL.aspx – Bridget the Midget Apr 08 '12 at 21:14
  • @BridgettheMidget Just read your question a few weeks/months later and see if it makes sense to you. – L.B Apr 08 '12 at 21:25
  • It makes sense to me. – Bridget the Midget Apr 17 '13 at 19:01

1 Answers1

10

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.

angularsen
  • 8,160
  • 1
  • 69
  • 83