如何防止 HtmlGenericControl 对内容进行 html 编码?
我正在使用 C# 编写一个 asp.net 站点(实际上是一个 DotNetNuke 模块)。从代码隐藏中,我尝试创建一个 标记,在其中设置所需的 js 变量,然后将其附加到 HMTL
。 js var 是图像文件相对 URL 的(文字)数组。由于数组包含字符串,因此每个项目都必须用引号引起来。
问题是, 和
之间的字符串在某处自动 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
查看使用 InnerHtml节点的属性,而不是 InnerText 属性。
Look at using the InnerHtml property of the node, not the InnerText property.