加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

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

发布时间:2020-12-16 09:57:16 所属栏目:asp.Net 来源:网络整理
导读:我有一个服务器控件,需要以编程方式将 JavaScript引用注入页面.它是引用Microsoft的Bing地图控件,它需要将 s = 1附加到脚本URL以供SSL使用.问题是.NET Framework对属性进行编码并更改一个 amp; (用Reflector验证).之后的某个时刻被完全删除. 期望的脚本标签
我有一个服务器控件,需要以编程方式将 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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读