显示从客户端检测到潜在危险的Request.QueryString值(search ="<html").在我的剃刀视图中

Sre*_*kat 2 razor asp.net-mvc-3

在我的控制器中,我在返回视图中为该特定操作提供了[ValidateInput(false)]我还附加了搜索关键字,我的搜索关键字是<html

我的网址看起来像 domainname/Clients?search = <html

在我看来

if (Request.QueryString.AllKeys.Contains("search"))
{
 string  search = Request.QueryString["search"].ToString();
}
Run Code Online (Sandbox Code Playgroud)

然后显示错误

从客户端检测到潜在危险的Request.QueryString值(search ="<html"). 如何在剃须刀视图中更正此错误?

Dar*_*rov 9

您需要requestValidationMode在web.config中设置为2.0:

<httpRuntime requestValidationMode="2.0" />
Run Code Online (Sandbox Code Playgroud)

或者使用视图模型和[AllowHtml]属性,在这种情况下,您只允许给定属性的那些字符:

public class SearchViewModel
{
    [AllowHtml]
    public string Search { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

和控制器动作:

public ActionResult Search(SearchViewModel model)
{
    if (!string.IsNullOrEmpty(model.Search))
    {
        string search = Model.Search;
    }

    ...
}
Run Code Online (Sandbox Code Playgroud)

在这种情况下,您既不需要[ValidateInput(false)]属性,也不需要requestValidationMode="2.0"web.config中的属性.

嘿,除此之外,您不再需要控制器动作中的魔术字符串:-)您正在直接使用模型.很酷,不是吗?