2012-04-08 37 views
4

爲什麼此URL在400中解析 - 錯誤請求?URL編碼的冒號解析爲400錯誤請求

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

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

%3A是一個URL編碼的冒號。

SOLUTION

這適用於某些原因:

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

+0

你是不是要寫「?search = s3%3Aabb」? – McGarnagle 2012-04-08 20:27:18

+0

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 2012-04-08 20:27:27

+0

@dbaseman不,我並不是故意寫這個。 – 2012-04-08 20:33:05

回答

6

看來,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通過「富:酒吧「作爲一個id參數,正確解碼。我剛剛用MVC4測試了它,它似乎工作。儘管它在問號之前並不接受URL編碼,但我確信這有一個很好的理由。可能在問號之前保留所有內容,並在問號之後標記有效的URL和任何參數。