Relative URLs

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • daveh551

    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.
  • Hans Kesting

    #2
    Re: Relative URLs

    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


    Comment

    • Leon Mayne

      #3
      Re: Relative URLs

      Hello

      "daveh551" <geekdh@gmail.c omwrote in message
      news:52da0670-e8ce-41d1-a239-2e2e46f4f530@m7 3g2000hsh.googl egroups.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


      Comment

      • daveh551

        #4
        Re: Relative URLs

        On Jul 24, 4:53 am, "Leon Mayne" <l...@rmvme.mvp s.orgwrote:
        Hello
        >
        "daveh551" <gee...@gmail.c omwrote in message
        >
        news:52da0670-e8ce-41d1-a239-2e2e46f4f530@m7 3g2000hsh.googl egroups.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.

        Comment

        • Leon Mayne

          #5
          Re: Relative URLs

          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)

          Comment

          • Hans Kesting

            #6
            Re: Relative URLs

            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


            Comment

            • daveh551

              #7
              Re: Relative URLs

              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.

              Comment

              Working...