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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14359305/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 11:35:26  来源:igfitidea点击:

MVC WEB API routing fails when url contains encoded ampersand

c#asp.net-mvcrestasp.net-web-apiasp.net-mvc-routing

提问by espvar

When i call my webservice witch takes two parameters i get:

当我打电话给我的网络服务时,我得到了两个参数:

A potentially dangerous Request.Path value was detected from the client (&).

从客户端 (&) 检测到潜在危险的 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 }
);

Controllermethod:

控制器方法:

[HttpGet]
public SearchResult Search(string category, string query)
{
}

When i call the api with:

当我调用 api 时:

/api/property/search/homes/areaId%3D20339%26areaId%3D20015

/api/property/search/homes/areaId%3D20339%26areaId%3D20015

A potentially dangerous Request.Path value was detected from the client (&).

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

Doing this:

这样做:

/api/property/search/homes/?query=areaId%3D20339%26areaId%3D20015

/api/property/search/homes/?query=areaId%3D20339%26areaId%3D20015

works fine.

工作正常。

How do i solve the routing decoding problem?

如何解决路由解码问题?

采纳答案by Darin Dimitrov

Scott Hanselman blogged about this. You might want to check the requestPathInvalidCharactersproperty of the <httpRuntime>node in your web.config.

Scott Hanselman就此发表了博客。您可能需要检查web.config 中节点的requestPathInvalidCharacters属性<httpRuntime>

Personally I would avoid such characters in the uri portion and simply put those values as query string parameters.

就我个人而言,我会避免在 uri 部分中使用此类字符,而只需将这些值作为查询字符串参数。