~ character

S

suzy

i have seen examples where the ~ character is used in the web.config file
when storing directories.

eg: ~/files/data.xml

what does that character mean?
 
S

S. Justin Gengo

The tilde character represents the root of the web site. It automatically
starts you at the base folder.

So for example, if your folder structure was: c:\inetpub\wwwroot\mysite

And inside of the site you had the folders:
pages
images

Normally if you were in the pages folder and needed to access an image you
would have to use "..":
<img src="../images/myimage.gif">

Of course if you had another folder inside of pages and you were in a file
there you'd need two sets of "..":
<img src="../../images/myimage.gif">

But using the tilde you automatically start at the root directory and don't
need to know how far back in the directory structure any folder is:
<img src="~/images/myimage.gif">

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 
S

S. Justin Gengo

After I posted I realized that my example is a little misleading. I
shouldn't have used an image tag as the example.

The tilde character is translated by .net server side as starting in the
root directory. If you use the tilde in client side code a web browser won't
know what it means.

The example I gave is good for how the structure works but using a tilde
client side in the example I gave would of course produce a broken link.

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 
L

Lostinet

~/ means ApplicationRoot when you use Control.ResolveUrl

(e.g. Image control use ResolveUrl to translate the url)
 
S

suzy

Hi,

I know about mapping to the root directory , but you don't need the tilda to
do that.

you can just use:

/files/data.xml

instead of:
~/files/data.xml

so I was just wondering what the tilda actually did.
 
M

Matt Berther

Hello suzy,
so I was just wondering what the tilda actually did.

Think of the ~ as an alias for the virtual root.

So, for example, you have a web application that is accessible via:

http://localhost/virtRoot1/Test.aspx

and in test.aspx, you have

<asp:HyperLink NavigateUrl="~/Test2.aspx"/>

the ~ would translate into /virtRoot1

I hope this clears things up...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top