我试图通过以下方式将HTML字符串作为参数传递:
$.ajax({
url: sourceUrl,
type: "GET",
cache: false,
success: function (data, status, jqxhr) {
etc...
}
其中sourceURL是对C#Controller操作的调用,其参数包含HTML字符串(来自富文本框)。类似的东西:
var sourceURL = "ThisController/ThisAction?Parameter=" + varWithHTML;
然而它没有说明:
从客户端
(description="<p>Rich Text String</p>")
检测到潜在危险的Request.QueryString值。
我试过在encodeURI()中包装字符串:
encodeURIComponent(varWithHTML)
但仍然是同样的问题。我还在web.config中对此问题提出了另一个建议:
<httpRuntime requestValidationMode="2.0" />
仍然没有运气。 我是疯了还是有办法解决这个问题?
答案 0 :(得分:2)
您应该使用POST方法并将html正文作为Post Body传递,同时您需要关闭ValidateInput
,您只需要将此行放在您的操作之上:
[ValidateInput(false)]//This line is to let you pass html in parameters.
public ActionResult ThisAction()
但要小心!这可能会使您的网站面临跨站点脚本攻击(XSS)的风险,这可能会让黑客在传递请求时轻松编辑该请求并在其中放入javascript。您可能需要手动处理,因为ValidateInput
是因为这个原因。
为了避免您需要通过Html编码传递输入并替换您想要仅允许的每个标记的每个括号,例如:
您只想启用代码<b> </b>
,然后您需要将<b>
替换为<b>
,将</b>
替换为</b>
,然后再将其保存在数据中或无论你想做什么!
[ValidateInput(false)]//This line is to let you pass html in parameters.
public ActionResult ThisAction(string htmlString)
{
StringBuilder sbHtmlString = new StringBuilder();
// Encode the htmlString.
sbHtmlString.Append(HttpUtility.HtmlEncode(htmlString));
// Only decode bold tag and ignore anything else!
sbHtmlString.Replace("<b>", "<b>");
sbHtmlString.Replace("</b>", "</b>");
htmlString = sbHtmlString.ToString();
//Do whatever you want with htmlString
return View();
}
祝你好运。