显示从客户端检测到具有潜在危险的请求。查询字符串值 (搜索 = "<html" )。在我的剃刀视图中



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

我的网址看起来像 域名/客户端?搜索=

在我看来

if (Request.QueryString.AllKeys.Contains("search"))
{
 string  search = Request.QueryString["search"].ToString();
}

然后显示错误

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

您需要

在web.config中将requestValidationMode设置为2.0:

<httpRuntime requestValidationMode="2.0" />

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

public class SearchViewModel
{
    [AllowHtml]
    public string Search { get; set; }
}

和控制器操作:

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

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

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

最新更新