AllowHtml不起作用

问题描述 投票:6回答:4

我正在构建一个内容管理系统,允许我以外的人更新网站上的内容。

我有一个面向前面的HTML表单,通过AJAX将数据发送到控制器:

// CONTROLLER
[ValidateInput(false)]
public void CarAJAX()
{
    CarAdmin CA = new CarAdmin();
    CA.UpdateCar(System.Web.HttpContext.Current.Request);
}

这些数据将包含HTML,因此我的模型中出现错误:

// MODEL
using System;
using System.Web;
using System.Web.Mvc;

namespace Site.Models
{
    public class CarAdmin
    {
        public String id { get; set; }
        [AllowHtml]
        public String HTML_Stuff { get; set; }

        public CarAdmin(){}

        public void UpdateCar(HttpRequest Request)
        {
            HTML_Stuff = Request.Form["HTML_Stuff"]; // <-- ERROR HAPPENS HERE!!!!!!

            // sanitation and validation

            String Select = String.Format("UPDATE Car Set HTML_Stuff = {0} WHERE id = {1}", HTML_Stuff, id);

            // Execute DB Command
        }
    }
}

如代码所示,当我尝试将成员设置为等于具有HTML的请求变量时,我收到错误。

编辑:错误是'检测到潜在危险的Request.Form值'

这是我尝试过的:

  • Change the validation mode in web.config,但我不想更改整个网站的验证,只有一个变量会有HTML。
  • [AllowHtml]在模型中,但我仍然得到同样的错误 - 好像[AllowHtml]什么也没做。
  • 在控制器中的[ValidateInput(false)],类似于AllowHtml,它似乎没有任何影响。

我在这里错过了什么吗?

c# asp.net-mvc-4
4个回答
12
投票

我有同样的问题。 “requestValidationMode =”2.0“”在web.config中设置,[AllowHtml]也设置在适当的属性上,我仍然收到错误“检测到潜在危险的Request.Form值...”。

但我观察到实际上调用了控制器方法(我能够调试该方法)所以这必然意味着验证实际上是关闭的。在调用堆栈中,我注意到反复出现类似于“System.Web.Caching.OutputCacheModule”的缓存类,这让我想到这与我在整个控制器上关闭的缓存有关,就像这样“[OutputCache( NoStore = true,持续时间= 0)]“。

基于此,我尝试将缓存的位置设置为OutputCacheLocation.None,这就是诀窍。所以我最终使用[OutputCache(NoStore = true,Duration = 0,Location = OutputCacheLocation.None)]工作,最后没有验证并且没有使我的请求失败。


0
投票

试试这个:

// CONTROLLER
[HttpPost]
public ActionResult CarAJAX(CarAdmin model)
{
    model.UpdateCar();
}

// MODEL
using System;
using System.Web;
using System.Web.Mvc;

namespace Site.Models
{
    public class CarAdmin
    {
        private string html;

        public String id { get; set; }
        [AllowHtml]
        public String HTML_Stuff { 
            get
            { 
                return html; 
            }
            set
            { 
                // sanitation and validation on "value"
                html = value;
            }
        }

        public CarAdmin(){}

        public void UpdateCar()
        {
            String Select = String.Format("UPDATE Car Set HTML_Stuff = {0} WHERE id = {1}", HTML_Stuff, id);

            // Execute DB Command
        }
    }
}

我还注意到你正在验证方法内部。如果你在设置属性时这样做可能会更好。

编辑: 我对这个话题进行了很多研究。您实际上需要使用AJAX将模型绑定到控制器。请看这个example。我不确定你的代码范围,但我认为你还需要ActionResult在控制器内返回。 There是从ActionResult返回什么的很好的例子。


0
投票

只需在控制器上放置[ValidateInput(false)]


-1
投票

你应该这样做 -

创建一个单独的类,其中包含所需的实体 -

public class EntityDto {
        public String id { get; set; }
        [AllowHtml]
        public String HTML_Stuff { get; set; }
}

然后在你的控制器方法中使用它 -

[ValidateInput(false)]
public void UpdateCar(EntityDto model)
{
    var html_stuff = model.HTML_Stuff; 

    // sanitation and validation

    String Select = String.Format("UPDATE Car Set HTML_Stuff = {0} WHERE id = {1}", html_stuff , id);

    // Execute DB Command
}

如果有帮助,请告诉我。

© www.soinside.com 2019 - 2024. All rights reserved.