【问题标题】:URL encoded colon resolves in 400 Bad RequestURL 编码冒号解析为 400 Bad Request
【发布时间】:2012-04-21 09:52:47
【问题描述】:

为什么这个 url 解析为 400 - Bad Request?

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

【问题讨论】:

  • 您的意思是写“?search=s3%3Aabb”吗?
  • @dbaseman 不,我不是故意的。
  • @BridgettheMidget,我猜你假设我们都知道这个Web.App.QuotesReaders 应用程序并且知道它的作用和作用
  • @L.B 你不需要。网址不带冒号。

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


【解决方案1】:

似乎 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 以及问号之后的任何参数。

【讨论】:

    猜你喜欢
    • 2014-09-01
    • 2017-09-02
    • 1970-01-01
    • 1970-01-01
    • 2019-02-24
    • 1970-01-01
    • 1970-01-01
    • 2011-08-31
    • 1970-01-01
    相关资源
    最近更新 更多