检测到潜在危险的Request.Form值

And*_*ewC 23 asp.net-mvc

我有一个带有wmd编辑器的表单.输入文本区域使用以下方式呈现:

<%: Html.TextAreaFor(t => t.NewsBody, new{@class="wmd-panel", id="wmd-input"}) %>
Run Code Online (Sandbox Code Playgroud)

我每次提交表格都会得到 A potentially dangerous Request.Form value was detected from the client

我尝试在action方法上设置[ValidateInput(false)],我尝试添加 <httpRuntime requestValidationMode="2.0" />到web.config并尝试validateRequest="false"在web.config中的pages指令中但它仍然在发生.

有任何想法吗?

编辑

行动方式:

 [ILFFAuthorize(Roles = "Admin")] // this is a custom auth attrobite
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult AddNews(FormCollection col){

        //public ActionResult AddNews(News news)
        //{
            if (ModelState.IsValid)
            {
                News news = new News();
                news.NewsDate = DateTime.Now;
                news.NewsPosterId = 0;

                news.NewsTitle = col["NewsTitle"];
                news.NewsBody = col["NewsBody"];
                newsRepository.Add(news);
                newsRepository.Save();

                return RedirectToAction("Index", "Home");
            }
            else
            {
                return View();
            }
        }
Run Code Online (Sandbox Code Playgroud)

Ste*_*vds 25

您需要将其置于[HttpPost]操作方法之上

    [HttpPost]
    [ValidateInput(false)]
    public ActionResult Edit(FormCollection collection) {
       .....
    }
Run Code Online (Sandbox Code Playgroud)

如果您正在使用MVC3,那么您不应该使用,[ValidateInput(false)]但请使用以下[AllowHtml]解释:http://dailydotnettips.com/2011/08/24/how-to-allow-user-to-input-html-in-asp- 净MVC /

另外:试着把[ValidateInput(false)]你高于你的[HttpPost]下方,我记得,这些都是从上到下执行的.


The*_*urt 6

在MVC 3中,添加[AllowHtml]到视图模型中要不进行验证的属性.