Question about ~

  • Thread starter Thread starter Danny Ni
  • Start date Start date
D

Danny Ni

Hello,

Can someone tell me where the following tag should be pointing to?

<asp:Image runat="server" ImageUrl="~/images/logo.gif" />

It renders in IE and FF as
<img src="images/logo.gif" ....>

I was expecting something like this
<img src="/cart/images/logo.gif" ....>

/cart is my virtual application root.

TIA
 
You are correct. "~" is a placeholder for the application path (application
root folder).

To render urls most web controls use paths relative to the directory
containing the current page.
When browser resolves such relative url it starts from the current page
location.

In your case if page is located in /cart folder (application root) then
~/images/logo.gif is resolved to images/logo.gif

Regards,

Alexander Myachin
 
Hello Danny,

Read about asp.net paths there http://msdn.microsoft.com/en-us/library/ms178116(VS.80).aspx

---
WBR,
Michael Nemtsev [.NET/C# MVP] :: blog: http://spaces.live.com/laflour

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo


DN> Hello,
DN>
DN> Can someone tell me where the following tag should be pointing to?
DN>
DN> <asp:Image runat="server" ImageUrl="~/images/logo.gif" />
DN>
DN> It renders in IE and FF as
DN> <img src="images/logo.gif" ....>
DN> I was expecting something like this
DN> <img src="/cart/images/logo.gif" ....>
DN> /cart is my virtual application root.
DN>
DN> TIA
DN>
 
Hi, Danny.

~ always refers to the application root.

If your application root is /cart, then the relative path
from /cart to ~/images/logo.gif is images/logo.gif.

re:
!> It renders in IE and FF as
!> <img src="images/logo.gif" ....>

Yes, that is the correct relative path.

re:
!> I was expecting something like this
!> <img src="/cart/images/logo.gif" ....>

That would be a path which includes the root directory.
Relative paths don't need to include the root directory.

If your root directory is /cart, then the relative path
/cart/images/logo.gif would point to /cart/cart/images/logo.gif





Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
======================================
 
Hello,

Can someone tell me where the following tag should be pointing to?

<asp:Image runat="server" ImageUrl="~/images/logo.gif" />

It renders in IE and FF as
<img src="images/logo.gif" ....>

I was expecting something like this
<img src="/cart/images/logo.gif" ....>

/cart is my virtual application root.

TIA

Hi Danny

Unless you state the path of the page containing the url no one can
predict specifically what the result will be.

For example if the site relative address of the page was /cart/
Apage.aspx then the result will be as already shown. If it was say /
cart/Afolder/Apage.aspx then the result will be ../images/logo.gif.

I must also stress that it is the server that does the translation of
application relative addresses (those using the ~ character) not the
browser, so it wont make any difference which browser you use. The ~
symbol would not be recognised on the client (besides the browser has
no way of knowing where in the full url the application root is). The
server replaces ~ with a normal W3C compliant relative address when
rendering the page (as in my exampe).

On receipt of a relative address the browser reconstructs the full
path based on the url of the requested page. So, for example, if
http://yourdomain.com/cart/Afolder/Apage.aspx returned an image with
an src="../images/logo.gif" it would send a request for the image file
to http://yourdomain.com/cart/images/logo.gif

HTH
 
Back
Top