about relative path in asp.net

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
Here is some text from a book that I read.
"One quirk that can catch unsuspecting developers is the way that master
pages handles relative paths. If all you're using is static text, this issue
won't affect you.. However, if you add <img> tag or any other HTML, tag that
points to another resource, problems can occur. The problem shows up if you
place the master page in a different directory from the content page that
uses it.. This is a recommended best practice for large websites. In fact
Microsoft encourages you to use a dedicated folder for storing all you
master pages. However if you're not suitably careful, this can cause
problems when you use relative paths. For example ,imagine you put a master
page in a subfolder named MasterPages and add the following <img> tag to the
master page: <img src="banner.jpg" /
Assuming the file \MasterPages\banner.jpg exists, this appears to work fins.
The image will even appear in the Visual Studio design environment. However,
if you create a content page in another subfolder, the image path is
interpreted relative to that folder. If the file doesn't exist there, you'll
get a broken link instead of your graphic. Even worse, you could conceivably
get the wrong graphic if another image has the same file name. This problem
occurs because the <img> tag is ordinary HTML. As a result ASP.NET won't
touch it. Unfortunately, when ASP.NET processes you content page, the
relative path in this tag is no longer appropriate. The same problem occurs
with <a>  tags that provide links to other pages and with the <link> element
that you can use to connect the master page to a style sheet.
To solve you problem, you could try to think ahead and write you URL
relative to the content page where you want to use it.. But this creates
confusion and limits where you master page can be used. A better fix is to
turn you <img> tag into a server-side control, in which case ASP.NET will
fix the mistake:
<img src="banner.jpg"  runat="server" />
This works because ASP:NEt uses this information to create an HTMLImage
server control. This object is created af ter the Page object for the master
page is instansiated. At this point ASP.NET interprets all the paths
relative to the location of the master page. And as with all server-side
control, you can further clear things up by using the ~/ character to create
a root-relative path. here's an example that clearly poins toa picture in an
Image folder in the root web application folder.
<img src="~/Images/banner.jpg" runat="server />"

Now to my question after this long text.  Can somebody explain in different
way then the book why the relative path is according to the Content page if
you use a HTML tag and the relative path is according tho the master page if
you use a server-side control ?

I hope to get a better explanation than the book


Re: about relative path in asp.net

Quoted text here. Click to load it

I think you would be better off taking this to one of the microsoft
online dot net groups.  This group is about authoring for the WWW using

Adrienne Boswell
Arbpen Web Site Design Services - http://www.cavalcade-of-coding.info /
The Good Plate - Fresh Gourmet Recipes - http://the-good-plate.com /
Please respond to the group so others can share

Re: about relative path in asp.net

On Sun, 2 Oct 2011 08:59:48 +0200, tony.johansson wrote:

Quoted text here. Click to load it

What makes a "relative path" *relative* is that it is interpreted
relative to some fixed directory position.

In an HTML file, a relative path like "../images/foo.jpg" tells
whatever is interpreting it to seek the parent ../ of whatever
directory the HTML file currently being read is in, change to
its subdirectory images/, and thence fetch the file foo.jpg.

A server-side control, though, may live rather elsewhere than where
the material for HTML it's going to serve resides, and so that control
needs to be told the path to your image foo.jpg relative to the
directory that *control* is housed in (it will then readjust that
path appropriately for the HTML file it's Actively Serving).

Or anyway, that's how I'm understanding the somewhat gobbledygookish
long text you quoted. Forgive me if I'm mistaken -- my attention span
was probably just not up to the task at hand :-) .

Cheers, -- tlvp
Avant de repondre, jeter la poubelle, SVP.

Re: about relative path in asp.net

tlvp wrote:

Quoted text here. Click to load it

That is oversimplified.  Actually, directories and files do not even enter
into it.  What matters is the path component of the URI, which can be quite
different from the actual server filesystem structure.  In

  http://foo.example/bar /

the URI-reference


means that the last component of the path, if any, is being removed –

  http://foo.example /

– and then the rest of the URI-reference is being appended:


[The difference is important because `../' in a HTML document resource named
bar.html, located in the document root, so accessible with
<http://foo.example/bar.html here, will _not_ access a directory outside of
the document root when triggered from the client side.  See below.]

Note that, contrary to popular belief, `/…' also is relative URI-reference
(see RFC 3986, section 4.1).  For absolute URIs always start with a `scheme'
component followed by a colon (e. g., `http:').  (Ibid., section 4.3.)
Quoted text here. Click to load it

In a HTML document, which can be thought of as being client-side, you have
URI paths used for requests; in a server-side program/script most of the
time you would deal with filesystem paths used for opening files.  URI paths
can map to filesystem paths but they do not have to.

For example, in a Web shop, the path


can map to a HTML document for which there is no file, one that is being
generated on-the-fly server-side using information from a database, while
the URI path is only provided for search engine optimization and user

For another example, in a Zend-Framework-based application the URI


could refer to a call of the detailAction() method of the CustomerController
class that is passed 42 for the `id' parameter, which in turn generates
(using the associated view) an (X)HTML document containing the details of
the customer with ID 42 in the database (using the associated model).  Aside
from the fact that there would be a CustomerController.php file, the URI
path has no basis in the filesystem whatsoever, and using `../' starting
from that document would be causing an error.

Quoted text here. Click to load it

Apparently.  I wished you would stop giving technical explanations or
recommendations until you increased it.

Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
  -- from <http://www.vortex-webdesign.com/help/hidesource.htm (404-comp.)

Re: about relative path in asp.net

On Tue, 11 Oct 2011 03:32:40 +0200, Thomas 'PointedEars' Lahn wrote:

Quoted text here. Click to load it

Thank you, Thomas, for filling in the much-needed gaps :-) .

Cheers, -- tlvp
Avant de repondre, jeter la poubelle, SVP.

Site Timeline