URL编码冒号在400 Bad Request中解析

时间:2012-04-08 20:23:51

标签: asp.net urlencode asp.net-mvc-4 asp.net-web-api

为什么这个网址解析为400 - 错误请求?

http://localhost:2785/api/ticker/Web.App.QuotesReaders/search=se%3Aabb

我的环境是Visual Studio 2010,MVC 4和使用的控制器是WebApiController。

%3A是一个URL编码的冒号。

这可以出于某种原因:

http://localhost:2785/api/ticker?className=Web.App.QuotesReaders&search=se%3Aabb

...这意味着,我无法在global.asax.cs中指定此路由:

/api/ticker/{className}/{search}

......也不是......

/api/ticker/{className}/search={search}

......但是......

/api/ticker

有关详情:http://www.hanselman.com/blog/ExperimentsInWackinessAllowingPercentsAnglebracketsAndOtherNaughtyThingsInTheASPNETIISRequestURL.aspx

1 个答案:

答案 0 :(得分:7)

似乎ASP.net在'?'之前不允许冒号在URL中,即使它被编码为%3A。

例如,这些不起作用

http://foo.org/api/persons/foo:bar http://foo.org/api/persons/foo%3abar

但这有效: http://foo.org/api/persons?id=foo%3abar

在所有示例中,我们希望ASP.NET MVC将“foo:bar”作为id参数传递,并正确解码。我刚刚用MVC4对它进行了测试,它似乎有效。令人讨厌的是它不接受问号之前的URL编码,但我确信它有充分的理由。可能要在问号前面保留一切有效的URL和问号后面的任何参数。

相关问题