How do I add a script url containing an ampersand with ASP.NET?

I have a server control that should programmatically inject a JavaScript link to a page. It must reference the Microsoft Bing control, which requires that it be &s=1added to the URL script for use over SSL. The problem is that the .NET Framework encodes attributes and changes &to &(checked using Reflector). At some point after that it is &completely removed.

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.ashx?v=6.2&s=1");
this.Page.Header.Controls.Add(include);

Any ideas?

+3
source share
3 answers

Desired script tag:

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

, . , script:

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

, & &amp;. ? HTML . ., , C.12. ( ) XHTML 1.0:

HTML XML, , , , (, "&amp;" ). , href a CGI script, , http://my.site.dom/cgi-bin/myscript.pl?class=guest&amp;name=user, http://my.site.dom/cgi-bin/myscript.pl?class=guest&name=user.

+3

... ? RegisterClientScript ilk, - , ​​ . aspx, ascx js.

? , . .

0

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

A quick fix is ​​to add the ScriptUrl property

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
source

Source: https://habr.com/ru/post/1731150/


All Articles