微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在ASP.NET中注入包含&符号的脚本URL?

我有一个服务器控件,需要以编程方式将 JavaScript引用注入页面.它是引用Microsoft的Bing地图控件,它需要将& s = 1附加到脚本URL以供SSL使用.问题是.NET Framework对属性进行编码并更改&一个& amp; (用Reflector验证).之后的某个时刻&被完全删除.

期望的脚本标签

<script type="text/javascript"
  src="https://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=6.2&s=1">
</script>

尝试1:

var clientScriptManager = this.Page.ClientScript;
if (!clientScriptManager.IsClientscriptincludeRegistered(this.GetType(),"BingMapControl"))
{
 clientScriptManager.RegisterClientscriptinclude(
  this.GetType(),"BingMapControl","https://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=6.2&s=1");
}

尝试2:

HtmlGenericControl include = new HtmlGenericControl("script");
include.Attributes.Add("type","text/javascript");
include.Attributes.Add("src","https://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=6.2&s=1");
this.Page.Header.Controls.Add(include);

有任何想法吗?

解决方法

Desired script tag:

<script type=”text/javascript”
src=”https://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=6.2&s=1″>
</script>

实际上,没有.实际上,您想要的脚本标记是:

<script type="text/javascript" 
    src="https://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=6.2&amp;s=1">
</script>

你确实想要&被编码为& amp ;.为什么?因为HTML标准是这样说的.例如,参见XHTML 1.0标准的第C.12. Using Ampersands in Attribute Values (and Elsewhere)节:

In order to ensure that documents are compatible with historical HTML user agents and XML-based user agents,ampersands used in a document that are to be treated as literal characters must be expressed themselves as an entity reference (e.g. “&amp;“). For example,when the href attribute of the a element refers to a CGI script that takes parameters,it must be expressed as http://my.site.dom/cgi-bin/myscript.pl?class=guest&amp;name=user rather than as http://my.site.dom/cgi-bin/myscript.pl?class=guest&name=user.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐