繁体   English   中英

在ASP.NET页中使用TinyMCE编辑器回发

[英]Postback with TinyMCE editor in ASP.NET page

我编写了这段代码,以在ASP页上使用TinyMCE (javascript“ richtext”编辑器)。 ASP页面本身包含一个名为“ art_content ”的文本框,该文本框生成一个诸如“ ctl00_hold_selectionblock_art_content ”的客户端ID

我遇到的一个问题是制作一个由我的应用程序存储的“安全”代码,并在我去客户端时加载回HTML,然后在提交(或回发)以防止HTML之前返回到“安全”模式在服务器端检查错误。

我的Javascript似乎正常运行,但无论如何我还是得到了错误。 尽管该函数正在触发,但看起来好像不起作用。

救命!

tinymce.init
(
  {
    selector:'#ctl00_hold_selectionblock_art_content',
    plugins:
      ['table link image textcolor pagebreak code visualblocks charmap'],
    toolbar:
      'bold,italic,underline'
  }
);
function SafeCms(text,encode)
{
  if(encode)
  {
    text=text.replaceAll('<','{{');
    text=text.replaceAll('>','}}');
  }
  else
  {
    text=text.replaceAll('{{','<');
    text=text.replaceAll('}}','>');
  }
  return text;
}
$(document).ready
(
  function()
  {
$('#ctl00_hold_selectionblock_art_content').val(SafeCms($('#ctl00_hold_selectionblock_art_content').val(),false));
    $("form").submit
    (
      function()
      {
        tinymce.triggerSave();
        $('#ctl00_hold_selectionblock_art_content').val(SafeCms($('#ctl00_hold_selectionblock_art_content').val(),true));
      }
    );
  }
);

更新:服务器端显示的错误(页面)

A potentially dangerous Request.Form value was detected from the client (ctl00$hold$selectionblock$art_content="<p>ab<em>acac</em>ac...").

CallStack:
[HttpRequestValidationException (0x80004005): Um valor possivelmente perigoso Request.Form foi detectado no cliente (ctl00$hold$selectionblock$art_content="<p>ab<em>acac</em>ac...").]
  System.Web.HttpRequest.ValidateString(String s, String valueName, String collectionName) +8818818
  System.Web.HttpRequest.ValidateNameValueCollection(NameValueCollection nvc, String collectionName) +111
  System.Web.HttpRequest.get_Form() +129
  System.Web.HttpRequest.get_HasForm() +8818919
  System.Web.UI.Page.GetCollectionBasedOnMethod(Boolean dontReturnNull) +97
  System.Web.UI.Page.DeterminePostBackMode() +63
  System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +6785
  System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +242
  System.Web.UI.Page.ProcessRequest() +80
  System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context) +21
  System.Web.UI.Page.ProcessRequest(HttpContext context) +49
  ASP.content_quality_knownledges_aspx.ProcessRequest(HttpContext context) in c:\Users\Sammuel\AppData\Local\Temp\Temporary ASP.NET Files\root\a3cdd555\dbee70c6\App_Web_e_7yzdu3.2.cs:0
  System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +181
  System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +75

本文中的建议,您可以在SaveContent事件中从编辑器内容中删除有问题的HTML字符。 如有必要,可以在BeforeSetContent事件中还原它们。

对于TinyMCE 4,您可以使用以下代码:

tinymce.init({
    selector: '#ctl00_hold_selectionblock_art_content',
    plugins: ['table link image textcolor pagebreak code visualblocks charmap'],
    toolbar: 'bold,italic,underline',
    setup: function (editor) {
        editor.on("SaveContent", function (e) {
            e.content = SafeCms(e.content, true);
        });
        editor.on('BeforeSetContent', function (e) {
            e.content = SafeCms(e.content, false);
        });
    }
});

function SafeCms(text, encode) {
    if (encode) {
        return text.replace(/</g, '{{').replace(/>/g, '}}');
    }
    else {
        return text.replace(/{{/g, '<').replace(/}}/g, '>');
    }
}

我使用的replace用正则表达式SafeCms代替replaceAll (不默认在Javascript中存在)。

我有此问题,请在您的web.config添加:

<system.web>
    <pages validateRequest="false" />
</system.web>

如果您使用的是.net 4.0,请确保将其添加到web.config

<httpRuntime requestValidationMode="2.0" />

实际上,您不需要手动替换字符,由于安全问题,您可能总会遇到您没有想到的特殊情况和ASP.NET块。

我认为最好的选择是对内容进行编码,然后在服务器端对其进行解码。

我将TinyMCE内容复制到一个不可见的文本框中(因此用户最终不会在其HTML编辑器中看到编码后的内容)

function encodeContent(copyToId, htmlContent) {
    var encodedHtml = encodeURIComponent(htmlContent);
    $('#' + copyToId).val(encodedHtml);
}

然后在服务器端,我只是获取此文本框的内容并对其进行解码

var decodedHtml = HttpUtility.UrlDecode(this.TextBoxNewContent.Text);

我不知道如何初始化TinyMCE,但我个人将其包装在我注册用于在Page_PreRender方法中调用的JS函数中。 实际上,我还有更多参数,这是一个简化的代码:

protected void Page_PreRender(object sender, EventArgs e)
{
    var script = "createHtmlEditor('"
       + this.tinyMceEditor.ClientID + "','"
       + this.TextBoxNewContent.ClientID + "');";

    var scriptId = "createHtmlEditor_" + this.tinyMceEditor.ClientID;
    ScriptManager.RegisterStartupScript(this, this.GetType(), scriptId, script, true);
}

encodeURIComponent是本机JavaScript函数,并且HttpUtility静态类位于System.Web命名空间中

注意:当我说“ invisible”文本框时,我是在说CSS invisible。 因为在ASP.NET中, Visibility="false"将导致未创建HTML元素!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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