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

发布于 2024-11-30 15:26:23 字数 1200 浏览 1 评论 0原文

我正在使用 C# 编写一个 asp.net 站点(实际上是一个 DotNetNuke 模块)。从代码隐藏中,我尝试创建一个

问题是, 之间的字符串在某处自动 HtmlEncoded,因此每个数组项周围的引号被替换为 "。这似乎是在渲染 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);

I'm writing an asp.net site (actually, a DotNetNuke module), using C#. From code-behind, I'm trying to create a <script> tag where I set a required js variable, then append it to the HMTL <head>. The js var is a (literal) array of relative urls for image files. Since the array contains strings, each item must be enclosed in quotes.

The problem is, the string between <script> and </script> is being automatically HtmlEncoded somewhere, so the quotes around each array item are being replaced with ". That seems to take place when the HtmlGenericControl is rendered. Could DotNetNuke be the culprit? Could someone suggest a workaround?

My current code (running from Page_Load handler in my control):

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北斗星光 2024-12-07 15:26:23

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

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

Look at using the InnerHtml property of the node, not the InnerText property.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文