asp.net-mvc - 如何避免 json <> MVC3 或 MVC4 出现 "Potentially dangerous request"错误?

标签 asp.net-mvc asp.net-mvc-3

我正在使用 Json 将更新信息发送到以下 Controller 操作:

    [HttpPost]
    public JsonResult JsonUpdate(string pk, string rk, string fld, string val) {
        Content content = null;
        try {
            if (fld == "TempRowKey") {
                content = contentService.Get(pk, rk);
                rk = utilityService.DotFormatToRowKey(val);
                contentService.UpdateRowKey(content, rk, User.Identity.Name);
            } else {

我意识到这是 MVC 中的附加保护导致了问题,但我需要能够在 val 参数中传递 HTML 代码,但我收到以下错误:

A potentially dangerous Request.Form value was detected from the client (val="...e HashMap <K,V>, LinkedHashMap..."). 

请注意,这是管理员输入的有效文本。该屏幕仅供管理员使用,因此我可以接受没有对该字段的保护。

看起来 MVC2、3 和 4 之间有一些变化。

使用 MVC3 和 MVC4 有哪些方法可以避免此问题。我在 stackoverflow 上看到了另一个像我这样的帖子,但它并没有真正解决问题。

请注意,我正在寻找可以应用于此操作的本地内容。我在网上看到了一些建议,但似乎在如何使用不同的 MVC 版本处理此问题之间存在混淆。是对数据进行编码和解码的最佳方法吗?如果是,我该怎么做?

最佳答案

[ValidateInput(false)]
public JsonResult JsonUpdate(string pk, string rk, string fld, string val)
{
    ...

但是,您也应该能够执行以下操作:

public class MyJsonUpdateModel
{
    public string pk { get; set; }
    public string rk { get; set; }
    public string fld { get; set; }

    [AllowHtml]
    public string val { get; set; }
}

// [ValidateInput(false)] not needed because model states HTML is allowed
public JsonResult JsonUpdate(MyJsonUpdateModel model)
{

关于asp.net-mvc - 如何避免 json <> MVC3 或 MVC4 出现 "Potentially dangerous request"错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11808304/

相关文章:

asp.net-mvc-3 - 在没有 HttpContext 或 ControllerContext 的情况下将 ASP.NET MVC 字符串呈现给 View?

html - IE 和 Edge 的网站 CSS 加载问题

c# - 当我的查询没有找到任何项目时,它应该返回 null 吗?

c# - html 帮助程序扩展方法上的多个通用参数

c# - 无 Cookie session

asp.net-mvc - 根据MVC中第一个下拉列表的值启用第二个和第三个下拉列表

jquery - 如何从 jQuery 数据表中的 ajax 数据源获取 mRender() 函数中的隐藏列值

asp.net-mvc - ASP.NET MVC - 存储库/服务/ Controller

asp.net-mvc - Automapper 映射列表变为 0

c# - Request.ServerVariables 抛出 NullReferenceException