简体   繁体   中英

A potentially dangerous Request.Form value was detected from the client (editor=“<div id=”header“ sty…”)

First of all I should say I have followed almost all the Questions and forum Post below

Stackoverflow Question 1

Stackoverflow Question 2

Stackoverflow Question 3

Stackoverflow Question 4

aspsnippets.com

Server Error in Application ... A potentially dangerous Request.Form value was detected

Avoiding the 'A potentially dangerous Request.Form value was detected'

c-sharpcorner.com

A potentially dangerous Request.Form value was detected from the client in asp.net

all the thread mentioned to add <httpRuntime requestValidationMode = "2.0" /> or <pages validateRequest ="false" /> inside the web.config file , but this isn't working for me .

Once I did that and start debugging , getting this kind of error

在此处输入图片说明

Actually I'm trying to is Loading a HTML file into Rich Text Editor content then Once I click Save as PDF button saving that Rich Text Editor content to PDF file

these are the relevant controller class methods

   [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() method

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();  
  }  

this is screenshot of error occurring place in PrepairEditor() method

在此处输入图片说明

output_xhtml.cshtml view file

<!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>  

Once I did that and start debugging , getting this kind of error

Look at the error you are getting. You already have a <httpRuntime /> section in your web.config. You can't have two of them. Instead of adding a new one, change the existing one.

It is because you are passing HTML

add: [AllowHtml] above your method

[AllowHtml] goes on the property in your model not the controller method. Its namespace is System.Web.MVC

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM