本文介绍了当 url 包含编码的 & 符号时,MVC WEB API 路由失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

路由配置:

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

控制器方法:

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

当我调用 api 时:

When i call the api with:

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

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

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

这样做:

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

工作正常.

如何解决路由解码问题?

How do i solve the routing decoding problem?

推荐答案

Scott Hanselman 发表了关于此的博客.您可能需要检查 web.config 中 节点的 requestPathInvalidCharacters 属性.

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

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

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

这篇关于当 url 包含编码的 & 符号时,MVC WEB API 路由失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 10:54