gpt4 book ai didi

c# - 如何防止 HtmlGenericControl 对内容进行 html 编码?

转载 作者:太空狗 更新时间:2023-10-29 19:58:47 27 4
gpt4 key购买 nike

我正在使用 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);

最佳答案

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

The InnerHtml property does not automatically encode special characters to and from HTML entities. HTML entities allow you to display special characters, such as the < character, that a browser would ordinarily interpret as having special meaning. The < character would be interpreted as the start of a tag and is not displayed on the page. To display the < character, you would need to use the entity <.

关于c# - 如何防止 HtmlGenericControl 对内容进行 html 编码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7127691/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com