当url包含编码的&符号时,MVC WEB API路由失败

esp*_*var 20 c# rest asp.net-mvc asp.net-mvc-routing asp.net-web-api

当我打电话给我的webservice时,我会得到两个参数:

从客户端(&)检测到潜在危险的Request.Path值.

Routeconfig:

config.Routes.MapHttpRoute(
name: "PropertiesSearch",
routeTemplate: "api/property/Search/{category}/{query}",
defaults: new { controller = "Property", action = "Search", category = "common", query = string.Empty }
);
Run Code Online (Sandbox Code Playgroud)

Controllermethod:

[HttpGet]
public SearchResult Search(string category, string query)
{
}
Run Code Online (Sandbox Code Playgroud)

当我打电话给api时:

/ API /属性/搜索/家庭/ areaID表示%3D20339%26areaId%3D20015

从客户端(&)检测到潜在危险的Request.Path值.

这样做:

/ API /属性/搜索/家庭/?查询= areaID表示%3D20339%26areaId%3D20015

工作良好.

如何解决路由解码问题?

Dar*_*rov 30

Scott Hanselman 在此博文中写道.您可能想要检查web.config中节点的requestPathInvalidCharacters属性<httpRuntime>.

我个人会在uri部分避免使用这些字符,只需将这些值作为查询字符串参数.

  • +1我个人会**避免在uri部分中使用**这样的字符,只需将这些值作为查询字符串参数.不能让它更大胆. (11认同)