开发者

How do I inject a script URL containing an ampersand with ASP.NET?

开发者 https://www.devze.com 2022-12-19 13:54 出处:网络
I have a server control that needs to programmatically inject a JavaScript reference into the page. It is to reference Microsoft\'s Bing map control which requires &s=1 to be appended to the scrip

I have a server control that needs to programmatically inject a JavaScript reference into the page. It is to reference Microsoft's Bing map control which requires &s=1 to be appended to the script URL for use over SSL. The problem is that the .NET Framework encodes the attributes and changes the & to an & (verified with Reflector). At some point after that the & is removed altogether.

Desired script tag:

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

Attempt 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");
}

Attempt 2:

HtmlGenericControl include = new HtmlGenericControl("script");
include.Attributes.Add("type", "text/javascript");
include.Attributes.Add("src",
 "https://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ash开发者_StackOverflow社区x?v=6.2&s=1");
this.Page.Header.Controls.Add(include);

Any ideas?


Desired script tag:

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

Actually, no. In fact, your desired script tag is:

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

You do want the & to be encoded as &amp;. Why? Because the HTML standard says so. See, for example, Section C.12. Using Ampersands in Attribute Values (and Elsewhere) of the XHTML 1.0 standard:

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.


Out of curiosity... is this code just an example? I generally only use RegisterClientScript and its ilk if there is some dynamic portion that needs to be set at runtime. Otherwise, you can just write it statically in an aspx, ascx, or js file.

Have you tried a Literal control? I know I've done this very thing recently. I'll have to dig up my code.


It seems like all controls added to Header are html.encode-d Page.Header.Controls.Add

Quick solution is to add a property ScriptUrl

public string ScriptUrl
{
get
{
return "https://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=6.2&s=1";
}
}

and in aspx

<head runat="server">
<title></title>
<script type="text/javascript" src="<%= ScriptUrl %>"></script>
</head>

And that's all

0

精彩评论

暂无评论...
验证码 换一张
取 消