如何从html编码内容中阻止HtmlGenericControl?

bfa*_*tto 7 c# asp.net dotnetnuke html-encode htmlgenericcontrol

我正在使用C#编写一个asp.net站点(实际上是一个DotNetNuke模块).从代码隐藏,我正在尝试创建一个<script>标签,我设置一个必需的js变量,然后将其附加到HMTL <head>.js var是图像文件的相对URL的(文字)数组.由于数组包含字符串,因此每个项必须用引号括起来.

问题是,之间的串<script></script>正在自动某处HtmlEncoded,让周围每个数组项目报价正在与更换&quot;.这似乎发生在HtmlGenericControl呈现时.DotNetNuke可能是罪魁祸首吗?有人可以建议一个解决方法吗?

我当前的代码(从Page_Load我控制中的处理程序运行):

HtmlGenericControl PreviewDataScriptTag = new HtmlGenericControl("script");
PreviewDataScriptTag.Attributes.Add("type", "text/javascript");
StringBuilder PreviewDataScriptCode = new StringBuilder();
PreviewDataScriptCode.Append("var preview_imgs = [");
string pathPrefix = @"""";
string pathSuffix = @""",";
foreach (string path in this.DocPreviewImages)
{
    PreviewDataScriptCode.Append(pathPrefix + PreviewUrlBase + Path.GetFileName(path) + pathSuffix);
}
// Remove last comma from js code
PreviewDataScriptCode.Remove(PreviewDataScriptCode.Length-1, 1);
PreviewDataScriptCode.Append("];");
PreviewDataScriptTag.InnerText = PreviewDataScriptCode.ToString();
Page.Header.Controls.Add(PreviewDataScriptTag);
Run Code Online (Sandbox Code Playgroud)

Amy*_*Amy 16

查看使用节点的InnerHtml属性,而不是InnerText属性.

InnerHtml属性不会自动对HTML实体中的特殊字符进行编码.HTML实体允许您显示特殊字符,例如浏览器通常会将其解释为具有特殊含义的<字符.<字符将被解释为标记的开头,并且不会显示在页面上.要显示<字符,您需要使用实体<.