我有一个 Web API 项目。我的端点之一允许进行字符串搜索,其中可能包含特殊字符。
[RoutePrefix("api/Search")]
[ValidateInput(false)] // this is *supposed* to allow us to search using "unsafe" characters, like %, & etc.
public class SearchController : ApiController
{
...
[HttpGet]
[Route("{searchValue}", Name = "GenericSearch")]
public async Task<IHttpActionResult> Search(string searchValue)
{
...
}
}
当我调用 api/Search/fred
时,它按预期工作。
当我调用 api/Search/fred%25
时,出现此错误:
[HttpException (0x80004005): A potentially dangerous Request.Path value was detected from the client (%).] System.Web.HttpRequest.ValidateInputIfRequiredByConfig() +561 System.Web.PipelineStepManager.ValidateHelper(HttpContext context) +54
尽管我的 Controller 装饰有 [ValidateInput(false)]
,并且根据我在其他地方找到的其他答案,我将 requestValidationMode
添加到我的 Web .配置:
<system.web>
...
<httpRuntime requestValidationMode="2.0" />
</system.web>
我还需要翻转什么 secret 开关?
最佳答案
您缺少 httpRuntime 上允许无效字符的属性。
<httpRuntime requestPathInvalidCharacters="" requestValidationMode="2.0" />
关于c# - 如何允许 "potentially dangerous"参数值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43869012/