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

标签 c# asp.net-mvc rest asp.net-web-api asp.net-mvc-routing

当我调用我的网络服务女巫时,我得到了两个参数:

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

路由配置:

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

Controller 方法:

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

当我调用 api 时:

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

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

这样做:

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

工作正常。

如何解决路由解码问题?

最佳答案

斯科特·汉塞尔曼 blogged about this .您可能需要检查 requestPathInvalidCharacters <httpRuntime> 的属性(property)web.config 中的节点。

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

关于c# - 当 url 包含编码的符号时,MVC WEB API 路由失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14359305/

相关文章:

C# 列表和内存

c# - 如何正确使用 Interlocked.Add()?

c# - MVC 5 自定义 AuthorizeAttribute

c# - 我应该学习 SharePoint 2010 还是 SharePoint 2007?

java - 无法在 Android studio 中的 woocommerce Rest api v2 中更新客户

java - 覆盖标准的 Spring Data REST API

PHP Delete 函数正在删除数据库行,但响应显示错误?

c# - Windsor LifeStyle - 每个图形的共享实例

c# - 有没有办法使用 C# 将 PST 文件导入 Outlook?

sql - 三层架构与存储库模式