2

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

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

在我看来

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

然后显示错误

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


您需要requestValidationMode在 web.config 中将 设置为 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)]属性,也不需要requestValidationMode="2.0"web.config 中的 。

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

4

1 回答 1

10

您需要requestValidationMode在 web.config 中将 设置为 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)]属性,也不需要requestValidationMode="2.0"web.config 中的 。

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

于 2012-06-21T10:31:45.073 回答