about relative path in asp.net

Discussion in 'ASP .Net' started by tony.johansson, Oct 1, 2011.

  1. Hello!
    '
    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

    //tony
     
    tony.johansson, Oct 1, 2011
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt
    Replies:
    3
    Views:
    19,567
    Sudsy
    Jul 8, 2004
  2. Thomas Guettler
    Replies:
    3
    Views:
    761
    Andrei
    Oct 27, 2003
  3. Oren
    Replies:
    1
    Views:
    551
    Michael D. Ober
    Apr 29, 2007
  4. chandan
    Replies:
    2
    Views:
    608
    chandan
    Dec 14, 2007
  5. Nigel Wilkinson
    Replies:
    2
    Views:
    389
    Nigel Wilkinson
    Jul 25, 2005
Loading...

Share This Page