Lee
Lee

Reputation: 8734

ASP.Net Control.ResolveUrl quirks

I have a weird issue. When resolving urls for script elements, the src element must be in ' '. With link elements the href must be in " ", or the code nugget gets rendered into html for some reason. The code snippet below illustrates the problem more clearly. Why is this the case?

<script src='<%:Page.ResolveUrl("~/JavaScript/jQuery/jquery-1.7.2.js") %>' type="text/javascript"/>

<link href="<%:Page.ResolveUrl("~/CSS/Foundation/foundation.css") %>" rel="stylesheet" type="text/css" />

Edit Just noticed the script element src tag works fine when enclosed in " " or ' ', but the link element href tag only seems to work when enclosed in " ".

Upvotes: 1

Views: 624

Answers (1)

Jeremy
Jeremy

Reputation: 9030

It doesn't work because you are using a : in your script. Instead, you should do this (and it will work using single quotes as well as double quotes:

<link href="<%= ResolveUrl("~/Styles/site.css")%>" rel="stylesheet" type="text/css" />

Observe above that I used a = rather than a :

Or, in some situations you can even use the # if you, say, called Page.Header.DataBind(); in the codebehind of a masterpage and you are outputing your references in the <head> of your masterpage:

<link href="<%# ResolveUrl("~/Styles/site.css")%>" rel="stylesheet" type="text/css" />

Upvotes: 1

Related Questions