473,387 Members | 1,882 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Relative URLs

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.
Jul 23 '08 #1
6 1596
on 24-7-2008, daveh551 supposed :
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
Jul 24 '08 #2
Hello

"daveh551" <ge****@gmail.comwrote in message
news:52**********************************@m73g2000 hsh.googlegroups.com...
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:HyperLinkcontrol, or add runat="server" (and an ID) to the
link:
<a href="~/Test.aspx" ID="lnkTest" runat="server">Test</a>
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

--
Leon Mayne
http://leon.mvps.org/

Jul 24 '08 #3
On Jul 24, 4:53 am, "Leon Mayne" <l...@rmvme.mvps.orgwrote:
Hello

"daveh551" <gee...@gmail.comwrote in message

news:52**********************************@m73g2000 hsh.googlegroups.com...
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:HyperLinkcontrol, or add runat="server" (and an ID) to the
link:
<a href="~/Test.aspx" ID="lnkTest" runat="server">Test</a>
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

--
Leon Maynehttp://leon.mvps.org/
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.
Jul 24 '08 #4
daveh551 wrote:
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)
Jul 25 '08 #5
daveh551 formulated on donderdag :
On Jul 24, 4:53 am, "Leon Mayne" <l...@rmvme.mvps.orgwrote:

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
Jul 25 '08 #6
On Jul 25, 3:41 am, Hans Kesting <news.han...@spamgourmet.comwrote:
daveh551 formulated on donderdag :
On Jul 24, 4:53 am, "Leon Mayne" <l...@rmvme.mvps.orgwrote:
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
Thanks to you both. I appreciate the help.
Jul 25 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

24
by: sinister | last post by:
After doing a websearch, it appears that it's OK to omit the "http:" to form a relative URL. Are there any pitfalls to this? For example, if there is a page http://www.domain1.com/page1.html...
2
by: abcd | last post by:
I am using https protocol and some of my pages are using relatie paths to our images folder and gif files... when the page is rendered the images are not rendered.....is there anything wrong...
10
by: tom | last post by:
hi group, i desperately need a function that will transform relative URLs to absolute URLs in the SRC part of <img> tags. ie: function makeAbsolute($html,$basehref) { //if regex match =...
2
by: daveh551 | last post by:
Okay, I asked a question a week or so ago asking for an explanation on relative URL's and the "~" symbol, and several people explained that the "~" is only usable when the URL is going to be parsed...
1
by: Nathan Sokalski | last post by:
I am using the System.Net.WebClient() and System.Text.UTF8Encoding() classes to get the output from another page of mine. My basic main algorithm is the following: ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.