473,785 Members | 2,419 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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\wwwr oot\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.Redire ct 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 1611
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.Redire ct 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=serv er" will not change this behaviour.

Hans Kesting
Jul 24 '08 #2
Hello

"daveh551" <ge****@gmail.c omwrote in message
news:52******** *************** ***********@m73 g2000hsh.google groups.com...
When I specify relative URLs in links and Response.Redire ct 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.Redire ct("~/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:HyperLinkc ontrol, 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.mvp s.orgwrote:
Hello

"daveh551" <gee...@gmail.c omwrote in message

news:52******** *************** ***********@m73 g2000hsh.google groups.com...
When I specify relative URLs in links and Response.Redire ct 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.Redire ct("~/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:HyperLinkc ontrol, 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>.asp x, 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>.asp x, 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.mvp s.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>.asp x, 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
"applicatio ns" 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...@sp amgourmet.comwr ote:
daveh551 formulated on donderdag :
On Jul 24, 4:53 am, "Leon Mayne" <l...@rmvme.mvp s.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>.asp x, 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
"applicatio ns" 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
4497
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 with a link to http://www.domain2.com/page2.html you can abbreviate the second link as //www.domain2.com/page2.html
2
1039
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 with https and relative urls....I am using Windows 2003...or may be permission issue somewhere...
10
3162
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 = relative URL ==> return img tag with absolute URL
2
3382
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 by ASP.NET. I have a TreeView control with a hierarchical Category listing of products. Here is the code in selected node changed event: protected void CategoryTreeView_SelectedNodeChanged(object sender, EventArgs e) {
1
1807
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: utf8.GetString(source.DownloadData(finalurl)) However, it keeps telling me Illegal characters in path.
0
9647
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10357
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10101
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9959
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7509
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6744
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5396
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5528
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.