AndreasKnudsen
AndreasKnudsen

Reputation: 3481

Why does urls get encoded in <link> tags within <head runat="server"> and how do I avoid it (asp.net)

(I have tested this with a vanilla asp.net site running from the webdev server and it is a problem here also):

I have the following markup in my .master file

<!DOCTYPE html>
<html>
<head runat="server">
    <link href="/Styles/Site.css" rel="stylesheet" type="text/css" />
    <link rel="alternate" type="application/rss+xml" title="rss" href="/Pages/Static/Feed.aspx?type=rss&lang=en" />    
</head>

the rendered html comes out like this:

<!DOCTYPE html>
<html>
<head>
    <link href="/Styles/Site.css" rel="stylesheet" type="text/css" />
    <link rel="alternate" type="application/rss+xml" title="asdsad" href="/Pages/Static/Feed.aspx?type=rss&amp;lang=en" />
</head>

(the rss link "&" has been encoded to "&")

however if i change the markup to

<!DOCTYPE html>
<html>
<head>
    <link href="/Styles/Site.css" rel="stylesheet" type="text/css" />
    <link rel="alternate" type="application/rss+xml" title="rss" href="/Pages/Static/Feed.aspx?type=rss&lang=en" />    
</head>

(no runat="server" on the head tag) then the resulting html comes out as expected:

<!DOCTYPE html>
<html>
<head runat="server">
    <link href="/Styles/Site.css" rel="stylesheet" type="text/css" />
    <link rel="alternate" type="application/rss+xml" title="rss" href="/Pages/Static/Feed.aspx?type=rss&lang=en" />    
</head>

Clearly Asp.Net does something to encode the url. As it happens, I really need the head tag to be runat="server" and I would also like to be able to have "&" in link-urls within it is there some trick I can use to have my cake and eat it too?

Yours Andreas

Upvotes: 0

Views: 1711

Answers (2)

Phill
Phill

Reputation: 18796

Note: If you're going to downvote, explain why, because nothing I have said is wrong, unless you otherwise prove so.

You're meant to escape ampersands in url's since putting a non-escaped ampersand in a url, the browser expects there to be something encoded. By escaping it, your telling the browser exactly what it is, an ampersand.

It doesn't break your links and is valid.

Also if you wanted to pass an actual ampersand in a url that isn't defining a new querystring paramter, then you would url encode the ampersand to be '%26'

Edit: Since you're probably using this in some really weird way. Here's the why, it's correct for ASP.Net to HTML Encode the ampersand for the HTML document.

When the browser issues a request for the URL, it doesn't send a request for the HTML encoded URL, it sends a request for the non-HTML encoded URL.

If for some reason you're accessing the value server-side or something, then you can do something like:

var url = new Uri(HttpUtility.HtmlDecode(@"http://www.google.com/somepage.aspx?key1=value1&key2=value2"));
var query = HttpUtility.ParseQueryString(url.Query);
var result = query["key2"];
Console.WriteLine(result);

So you decode the HTML version of the link first, parse it as a Uri, get the querystring from it and key your key/value collection.

Upvotes: 1

Dan
Dan

Reputation: 2341

This issue happened to me before, I couldn't find out the reason for this, I ended up putting a literal inside the head, and filled the html from the code behind. html:

<head runat="server">
    <asp:Literal runat="server" ID='litLinks' />
</head>

C# code:

protected void Page_Load(object sender, EventArgs e)
{
  litLinks.Text = "<link rel='alternate' type='application/rss+xml' title='rss' href='/Pages/Static/Feed.aspx?type=rss&lang=en' />"
                  + "<link href='/Styles/Site.css' rel='stylesheet' type='text/css' />";
}

Upvotes: 3

Related Questions