-1

首先我应该说我已经关注了下面几乎所有的问题和论坛帖子

堆栈溢出问题 1

堆栈溢出问题 2

堆栈溢出问题 3

堆栈溢出问题 4

aspsnippets.com

应用程序中的服务器错误...检测到潜在危险的 Request.Form 值

避免“检测到潜在危险的 Request.Form 值”</a>

c-sharpcorner.com

在 asp.net 中从客户端检测到潜在危险的 Request.Form 值

所有提到的要添加 <httpRuntime requestValidationMode = "2.0" /><pages validateRequest ="false" />在 web.config 文件中的线程,但这对我不起作用。

一旦我这样做并开始调试,就会出现这种错误

在此处输入图像描述

实际上,我正在尝试将 HTML 文件加载到富文本编辑器内容中,然后单击另存为 PDF按钮将富文本编辑器内容保存到 PDF 文件

这些是相关的控制器类方法

   [ValidateInput(false)]  
      public ActionResult output_xhtml()  
      {  
          PrepairEditor(delegate(Editor editor)  
          {  
              editor.LoadHtml("~/example.html");  
          });  
          return View();  
      }  

      [HttpPost]  
      [ValidateInput(false)]  
      public ActionResult output_xhtml(string m)  
      {  
          Editor theeditor = PrepairEditor(delegate(Editor editor)  
          {  

          });  

          theeditor.SavePDF("~/aaa.pdf");  

          return View();  
      }

PrepairEditor() 方法

protected Editor PrepairEditor(Action<Editor> oninit)  
  {  
      Editor editor = new Editor(System.Web.HttpContext.Current, "editor");  

      editor.ClientFolder = "/richtexteditor/";  
      editor.ContentCss = "/Content/example.css";  
      //editor.ClientFolder = "/Content/richtexteditor/";      
      //editor.ClientFolder = "/Scripts/richtexteditor/";      

      editor.Text = "Type here";  

      editor.AjaxPostbackUrl = Url.Action("EditorAjaxHandler");  

      if (oninit != null) oninit(editor);  

      //try to handle the upload/ajax requests      
      bool isajax = editor.MvcInit();  

      if (isajax)  
          return editor;  

      //load the form data if any      
      if (this.Request.HttpMethod == "POST")  
      {  
          string formdata = this.Request.Form[editor.Name];  
          if (formdata != null)  
              editor.LoadFormData(formdata);  
      }  

      //render the editor to ViewBag.Editor      
      ViewBag.Editor = editor.MvcGetString();  

      return editor;  
  }  

  //this action is specified by editor.AjaxPostbackUrl = Url.Action("EditorAjaxHandler");      
  //it will handle the editor dialogs Upload/Ajax requests      
  [ValidateInput(false)]  
  public ActionResult EditorAjaxHandler()  
  {  
      PrepairEditor(delegate(Editor editor)  
      {  

      });  
      return new EmptyResult();  
  }  

这是 PrepairEditor() 方法中发生错误的地方的截图

在此处输入图像描述

output_xhtml.cshtml视图文件

<!DOCTYPE html>
<html>
<head>
    <title>RichTextEditor - Output XHTML</title>    
</head>
<body>

    <script type="text/javascript">

    var editor;

    function RichTextEditor_OnLoad(editor) {
        editor = editor;
            var content = true;
            if (!content) {
                setTimeout(function () {
                    editor.SetText("<table>.....</table>");
                }, 1000);
                return;
            }
        }

    </script>


    <script type='text/javascript'>

    function RichTextEditor_OnLoad(editor) {
        editor.SetWidth(1150); //Sets the width.
        editor.SetHeight(612); //Sets the height.
    }

    </script>

    @using (Html.BeginForm())
    {           

            <div>
                @Html.Raw(ViewBag.Editor)
                <br />
                <button id="btn_sumbit" type="submit" class="btn btn-danger submit">Save as PDF</button>                   
            </div>
            <br />
            <div>
                <h3>
                    Result html:
                </h3>
                <div>
                    @ViewBag._content
                </div>
            </div>
    }
</body>
</html>  

这是因为您正在传递 HTML

添加:[AllowHtml]在您的方法上方

4

3 回答 3

2

一旦我这样做并开始调试,就会出现这种错误

看看你得到的错误。您的 web.config 中已经有一个<httpRuntime />部分。你不能有两个。不要添加新的,而是更改现有的。

于 2015-10-15T09:18:38.970 回答
1

这是因为您正在传递 HTML

添加:[AllowHtml]在您的方法上方

于 2015-10-15T08:58:20.133 回答
0

[AllowHtml] 继续模型中的属性而不是控制器方法。它的命名空间是 System.Web.MVC

于 2019-10-10T20:43:10.947 回答