Relative URLs

D

daveh551

I need some help (and my apologies if this is not the right group)
understanding how to properly form Relative URL's.

I'm developing a site (Web Site, not a Web Application if that makes a
difference) in VS 2005. The application is called OBS, and it's
located C:\Inetpub\wwwroot\OBS. In IIS, it shows a Virtual Directory
at that location, running an application "OBS", and with a Starting
point "<Default Web Site>\OBS".

When I specify relative URLs in links and Response.Redirect calls,
I've been specifying it as "~/PageName.aspx", or whatever. Most of
the time that works, but sometimes I will get a 404 Resource not found
error saying that it could not locate the resource "/OBS/~/
PageName.aspx" I was under the impression that the "~" stood for the
root, i.e., OBS. So why is it constructing the resource name that
way?

Also, I thought that "/PageName.aspx" should be equivalent to "~/
PageName.aspx", but it clearly is not, since that fails almost all the
time.

In one case (which seems to have stopped working that way now, for
some unknown reason), the Redirect worked correctly when it was run
from the Default page, but when it was run from a different page
(still at the top level), it failed in the above manner.

I seem to be able to get it work if I put the link in as
"PageName.aspx", but I know that will break if I end up moving pages
around to different directories, so I want to avoid that .

Can anyone tell me anything that will shed some light on my obvious
confusion?

Thanks in advance for any help.
 
H

Hans Kesting

I need some help (and my apologies if this is not the right group)
understanding how to properly form Relative URL's.

When I specify relative URLs in links and Response.Redirect calls,
I've been specifying it as "~/PageName.aspx", or whatever. Most of
the time that works, but sometimes I will get a 404 Resource not found
error saying that it could not locate the resource "/OBS/~/
PageName.aspx" I was under the impression that the "~" stood for the
root, i.e., OBS. So why is it constructing the resource name that
way?

Also, I thought that "/PageName.aspx" should be equivalent to "~/
PageName.aspx", but it clearly is not, since that fails almost all the
time.

Can anyone tell me anything that will shed some light on my obvious
confusion?

Thanks in advance for any help.

The "~" represents the root of your web-application, but *only* if that
URL is processed by the asp.net system before sending it to the
browser.

Some examples:
<img src="~/myimage.gif"/>
will not be found. This is "plain text markup" as far as asp.net is
concerned and is sent "as is" to the browser. And that browser knows
nothing about "web applications" or that a "~" could have a special
meaning.

<img src="~/myimage.gif" runat=server/>
Now you created an HtmlControl, so the "src" *is* processed by
asp.net which replaces it with the current application root before
sending it to the browser.

<img src="/myimage.gif"/>
Here the image will always be searched in the root of the *server*,
which may or may not be the same as the root of your application.
Adding a "runat=server" will not change this behaviour.

Hans Kesting
 
L

Leon Mayne

Hello

daveh551 said:
When I specify relative URLs in links and Response.Redirect calls,
I've been specifying it as "~/PageName.aspx", or whatever. Most of
the time that works, but sometimes I will get a 404 Resource not found
error saying that it could not locate the resource "/OBS/~/
PageName.aspx" I was under the impression that the "~" stood for the
root, i.e., OBS. So why is it constructing the resource name that
way?

The tilde (~) is a server parsed shorthand for the "home" directory of the
application. This is determined by ASP.NET and the browser does not know
what to do with it. Therefore anywhere you use ~/ you have to make sure it
is being parsed by the server, e.g. in the code behind:
Response.Redirect("~/Test.aspx")
Is fine, because this is server parsed. In an aspx file:
<a href="~/Test.aspx">Test</a>
Will not work, because this is not parsed by the server and the client will
just try to append the tilde onto the current path. To rectify this, either
use an <asp:HyperLink> control, or add runat="server" (and an ID) to the
link:
Also, I thought that "/PageName.aspx" should be equivalent to "~/
PageName.aspx", but it clearly is not, since that fails almost all the
time.

No, "/" means the root of the website, so if you are in /OBS/TestDir/ and
you reference "/pagename.aspx" then it will look for it under "/", not
"/OBS", regardless of whether the referencing control is being server parsed
or not.

HTH
 
D

daveh551

Hello




The tilde (~) is a server parsed shorthand for the "home" directory of the
application. This is determined by ASP.NET and the browser does not know
what to do with it. Therefore anywhere you use ~/ you have to make sure it
is being parsed by the server, e.g. in the code behind:
Response.Redirect("~/Test.aspx")
Is fine, because this is server parsed. In an aspx file:
<a href="~/Test.aspx">Test</a>
Will not work, because this is not parsed by the server and the client will
just try to append the tilde onto the current path. To rectify this, either
use an <asp:HyperLink> control, or add runat="server" (and an ID) to the
link:


No, "/" means the root of the website, so if you are in /OBS/TestDir/ and
you reference "/pagename.aspx" then it will look for it under "/", not
"/OBS", regardless of whether the referencing control is being server parsed
or not.

HTH

Thanks a lot to both of you. That clears up a lot. Especially why it
works someplaces and not others!

But it leaves a question. IS there a notation that will get me the
root of the application, (not the root of the website) regardless of
whether it is parsed by ASP or not? If I make everylink be /OBS/
<something>.aspx, then I'm going to have to change every link when I
move it my hosting service.

Thanks.
 
L

Leon Mayne

daveh551 said:
But it leaves a question. IS there a notation that will get me the
root of the application, (not the root of the website) regardless of
whether it is parsed by ASP or not? If I make everylink be /OBS/
<something>.aspx, then I'm going to have to change every link when I
move it my hosting service.

Unfortunately not, because only your ASP.NET app knows where the root of
the application is. If the link is not parsed then it's down to the
browser to try and work it out. You could use the Visual Studio regex
search to find all the links that need changing. You just need to add
runat="server" to them.

The only thing I can suggest for the future is to use relative links
wherever possible (e.g. on pages which link to pages in the same folder)
and use parsed links whenever you will not be able to determine the URL
of the calling page (such as master pages, web user controls etc)
 
H

Hans Kesting

daveh551 formulated on donderdag :
But it leaves a question. IS there a notation that will get me the
root of the application, (not the root of the website) regardless of
whether it is parsed by ASP or not? If I make everylink be /OBS/
<something>.aspx, then I'm going to have to change every link when I
move it my hosting service.

Thanks.

Unfortunately not. The browser always needs to request a full URL (even
if it is specified as a relative URL) and knows nothing about
"applications" or their roots.

One trick that you CAN use: Have all your pages at the same "directory
depth", so that you always can use the same relative URL to get to the
root of your application. From there you can point to your resource.

An example: if all your pages are two directories deep (like
<approot>/MainPart/SubPart/thePage.aspx), then you can always use
"../../" to get from your page to the application root.
So to point to an "icon.gif" in an "images" directory in the root of
your application, use src="../../images/icon.gif".

Hans Kesting
 
D

daveh551

daveh551 formulated on donderdag :




Unfortunately not. The browser always needs to request a full URL (even
if it is specified as a relative URL) and knows nothing about
"applications" or their roots.

One trick that you CAN use: Have all your pages at the same "directory
depth", so that you always can use the same relative URL to get to the
root of your application. From there you can point to your resource.

An example: if all your pages are two directories deep (like
<approot>/MainPart/SubPart/thePage.aspx), then you can always use
"../../" to get from your page to the application root.
So to point to an "icon.gif" in an "images" directory in the root of
your application, use src="../../images/icon.gif".

Hans Kesting

Thanks to you both. I appreciate the help.
 

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

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top