Relative URLs

Discussion in 'ASP .Net' started by daveh551, Jul 23, 2008.

  1. daveh551

    daveh551 Guest

    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.
     
    daveh551, Jul 23, 2008
    #1
    1. Advertising

  2. daveh551

    Hans Kesting Guest

    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
     
    Hans Kesting, Jul 24, 2008
    #2
    1. Advertising

  3. daveh551

    Leon Mayne Guest

    Hello

    "daveh551" <> wrote in message
    news:...
    > 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:
    <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/
     
    Leon Mayne, Jul 24, 2008
    #3
  4. daveh551

    daveh551 Guest

    On Jul 24, 4:53 am, "Leon Mayne" <> wrote:
    > Hello
    >
    > "daveh551" <> wrote in message
    >
    > news:...
    >
    > > 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:
    > <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.
     
    daveh551, Jul 24, 2008
    #4
  5. daveh551

    Leon Mayne Guest

    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)
     
    Leon Mayne, Jul 25, 2008
    #5
  6. daveh551

    Hans Kesting Guest

    daveh551 formulated on donderdag :
    > On Jul 24, 4:53 am, "Leon Mayne" <> 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.
    >
    > 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
     
    Hans Kesting, Jul 25, 2008
    #6
  7. daveh551

    daveh551 Guest

    On Jul 25, 3:41 am, Hans Kesting <> wrote:
    > daveh551 formulated on donderdag :
    >
    > > On Jul 24, 4:53 am, "Leon Mayne" <> 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.

    >
    > > 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.
     
    daveh551, Jul 25, 2008
    #7
    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. abcd

    https and relative urls

    abcd, Aug 2, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    442
  2. Kaidi
    Replies:
    5
    Views:
    475
    Andrew Thompson
    Jan 4, 2004
  3. Nathan Sokalski

    Converting Relative URLs into Absolute URLs

    Nathan Sokalski, Aug 11, 2008, in forum: ASP .Net
    Replies:
    1
    Views:
    749
    Sriram Srivatsan
    Aug 12, 2008
  4. Adam Monsen

    JDBC URLs ...not really URLs?

    Adam Monsen, Feb 6, 2009, in forum: Java
    Replies:
    11
    Views:
    6,226
    Adam Monsen
    Feb 8, 2009
  5. Steve T.

    dynamic URLS convert to static URLS for search engines

    Steve T., Mar 1, 2004, in forum: ASP .Net Web Services
    Replies:
    7
    Views:
    293
    Steve T.
    Mar 4, 2004
Loading...

Share This Page