需要使用字符串参数解释Web Api方法的行为

时间:2014-03-04 13:07:25

标签: asp.net-mvc asp.net-web-api asp.net-mvc-5.1

我有WebApi方法,带有字符串参数:

public IEnumerable<Foo> Get(string stuff)
{
  //do stuff
}

路线:

config.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{stuff}",
    defaults: new { stuff= RouteParameter.Optional }
);

如果我这样调用方法: http://localhost:13892/api/servce?stuff=https://mysite.com/bla?g=1 - 一切顺利

但如果我去 http://localhost:13892/api/servce/https://mysite.com/bla?g=1 - 收到错误: A potentially dangerous Request.Path value was detected from the client (:).

知道如何解决该错误,但我想知道为什么会发生这种情况? 为什么在第一种情况下我没有收到该错误

1 个答案:

答案 0 :(得分:1)

一个简单的解释是,参数中包含的字符:不允许作为URL的一部分,但在查询字符串中使用是完全合法的

请注意,这可能是A potentially dangerous Request.Path value was detected from the client (*)

的重复内容