Resolve url problem with asp.net tilde ("~")

Discussion in 'ASP .Net' started by ibiza, Feb 19, 2006.

  1. ibiza

    ibiza Guest

    Hi all,

    I have a problem which I don't understand with the "~" mark in a
    NavigateUrl property of an asp:HyperLink. I have this in a master page,
    to build the standard menu (rMenuItems is a Repeater):

    Protected Sub rMenuItems_ItemDataBound(ByVal sender As Object, ByVal e
    As System.Web.UI.WebControls.RepeaterItemEventArgs)
    If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType =
    ListItemType.AlternatingItem Then
    CType(e.Item.FindControl("lnk"), HyperLink).NavigateUrl =
    DataBinder.Eval(e.Item.DataItem, "Url")
    End If
    End Sub

    It works well, if the url in the browser is at root. Example, if I am
    at : http://localhost/faq.aspx, it works.

    As soon as I am not in the root directory, e.g.:
    http://localhost/test/test.aspx, the links now all point to a location
    in the subfolder. e.g.: the link who was mapped with "~/default.aspx"
    points at http://localhost/test/default.aspx in the browser.

    Anyone knows the correction for this one?...

    thanks!

    ibiza
    ibiza, Feb 19, 2006
    #1
    1. Advertising

  2. ibiza

    Lloyd Dupont Guest

    I think that "~" is not very useful.
    Better use: "~/"
    i.e. "~/default.aspx" instead of "~default.aspx"

    "ibiza" <> wrote in message
    news:...
    > Hi all,
    >
    > I have a problem which I don't understand with the "~" mark in a
    > NavigateUrl property of an asp:HyperLink. I have this in a master page,
    > to build the standard menu (rMenuItems is a Repeater):
    >
    > Protected Sub rMenuItems_ItemDataBound(ByVal sender As Object, ByVal e
    > As System.Web.UI.WebControls.RepeaterItemEventArgs)
    > If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType =
    > ListItemType.AlternatingItem Then
    > CType(e.Item.FindControl("lnk"), HyperLink).NavigateUrl =
    > DataBinder.Eval(e.Item.DataItem, "Url")
    > End If
    > End Sub
    >
    > It works well, if the url in the browser is at root. Example, if I am
    > at : http://localhost/faq.aspx, it works.
    >
    > As soon as I am not in the root directory, e.g.:
    > http://localhost/test/test.aspx, the links now all point to a location
    > in the subfolder. e.g.: the link who was mapped with "~/default.aspx"
    > points at http://localhost/test/default.aspx in the browser.
    >
    > Anyone knows the correction for this one?...
    >
    > thanks!
    >
    > ibiza
    >
    Lloyd Dupont, Feb 19, 2006
    #2
    1. Advertising

  3. ibiza

    ibiza Guest

    Hi, thanks for the reply. This is however exactly how I use it...

    Actually, my problem is that the tilde does not seem to point at
    the root of the application, which is what I want. To clarify, if I am
    on a page at the root, like http://localhost/index.aspx, my menu link
    which has the NavigateUrl as "~/faq,aspx" correctly points at
    http://localhost/faq.aspx, which is correct in that case.

    However, as soon as I am in a page in a sub directory, like
    http://localhost/test/test.aspx, the same link which the NavigateUrl
    still is "~/faq,aspx" now points to http://localhost/test/faq.aspx,
    which is incorrect.

    The tilde does not seem to be taken in count, how is that?
    ibiza, Feb 19, 2006
    #3
  4. ibiza

    ibiza Guest

    This is how I use it.

    Actually, my problem is that the tilde does not seem to point at
    the root of the application, which is what I want. To clarify, if I am
    on a page at the root, like http://localhost/index.aspx, my menu link
    which has its NavigateUrl as "~/faq,aspx" correctly points at
    http://localhost/faq.aspx, which is correct in that case.

    However, as soon as I am in a page in a sub directory, like
    http://localhost/test/test.aspx, the same link, which the NavigateUrl
    is
    still "~/faq,aspx", now points to http://localhost/test/faq.aspx,
    which is incorrect.

    The tilde does not seem to be taken in count, how is that?
    ibiza, Feb 19, 2006
    #4
  5. ibiza

    Riki Guest

    ibiza wrote:
    > This is how I use it.
    >
    > Actually, my problem is that the tilde does not seem to point at
    > the root of the application, which is what I want. To clarify, if I am
    > on a page at the root, like http://localhost/index.aspx, my menu link
    > which has its NavigateUrl as "~/faq,aspx" correctly points at
    > http://localhost/faq.aspx, which is correct in that case.
    >
    > However, as soon as I am in a page in a sub directory, like
    > http://localhost/test/test.aspx, the same link, which the NavigateUrl
    > is
    > still "~/faq,aspx", now points to http://localhost/test/faq.aspx,
    > which is incorrect.
    >
    > The tilde does not seem to be taken in count, how is that?


    The tilde indeed refers to the root of the application. This can
    be the root of the web site, but not always.
    If you created a virtual directory, the tilde will refer to that
    virtual directory, because that will be considered the root of
    the application.

    To summarize:
    / refers to the root of the web site (http://localhost/)
    ~ refers to the root of the application (http://localhost/test/ if test is
    a virtual directory, http://localhost/ if test is not a virtual directory)

    --

    Riki
    Riki, Feb 19, 2006
    #5
  6. This is not accurate. "~" represents the root of any application,
    regardless of where it's located. If you have a subdirectory of a website
    that is also configured as an application in IIS, then "~" can be used to
    represent that subdirectory in the context of its own IIS application.

    For example, if you are in the page http://localhost/fax.aspx and you have a
    link to ~/test/test.aspx, the tilde represents the current application,
    http://localhost; on the other hand, if you are in the page
    http://localhost/test/test.aspx and you have a link to ~/faq.aspx, since
    http://localhost/test is also an IIS application, then it assumes that this
    is what represents the tilde and thinks that faq.aspx is in the test
    subdirectory/subweb.
    --
    Christopher A. Reed
    "The oxen are slow, but the earth is patient."

    "Riki" <> wrote in message
    news:...
    > To summarize:
    > / refers to the root of the web site (http://localhost/)
    > ~ refers to the root of the application (http://localhost/test/ if test is
    > a virtual directory, http://localhost/ if test is not a virtual directory)
    Christopher Reed, Feb 19, 2006
    #6
  7. ibiza

    ibiza Guest

    ok, thanks for the follow-ups.

    But my problem is still present, as in my example, /test is not
    configured as an application, in fact the directory does not even
    exists! I used a custom error 404 page to redirect correctly, according
    to the url (e.g.: if I type http://localhost/faq, I do a
    server.transfer to http://localhost/faq.aspx, and if I type
    http://localhost/test/yeah, I will be redirected to
    http://localhost/test.aspx?dir=yeah).

    To resume, my menu link with its NavigateUrl property set to "~/faq"
    (now that I explained that I have custom error page redirection)
    correctly redirects me to the correct "http://localhost/faq" page if
    the url in my browser is not a subdirectory and redirects me to the
    incorrect/inexistent "http://localhost/SUBDIR/faq" page if the url in
    my browser contains a subdirectory.

    I included an example of a correct and incorrect scenario screenshots,
    depending on the url in the browser to help better understand.

    http://img380.imageshack.us/img380/7554/tilt7om.gif

    Hope this is enoughe to solve my problem!

    Thanks a lot all for your time
    ibiza, Feb 19, 2006
    #7
  8. ibiza

    the4man Guest

    I have *exactly* the same problem. But my problem started one day ago,
    when I decided to use the output cache in my web CONTROLS. ┬┐Are you
    using cache?

    When you use the OutputCache directive, the first time the urls are
    created well. You know, if you are in the root directory, and you have
    a link like this "~/test/test.aspx", it will be transformed in
    "test/test.aspx". But if you are in a page inside a subdirectory, and
    the link is in a control that have been cached....the link won't be
    rendered (its got from the cache), so you are using yet
    "test/test.aspx" when you need "subdir/test/test.aspx".

    Can this be your problem? Are you using cache in any way?

    Bye!!!

    ibiza ha escrito:

    > ok, thanks for the follow-ups.
    >
    > But my problem is still present, as in my example, /test is not
    > configured as an application, in fact the directory does not even
    > exists! I used a custom error 404 page to redirect correctly, according
    > to the url (e.g.: if I type http://localhost/faq, I do a
    > server.transfer to http://localhost/faq.aspx, and if I type
    > http://localhost/test/yeah, I will be redirected to
    > http://localhost/test.aspx?dir=yeah).
    >
    > To resume, my menu link with its NavigateUrl property set to "~/faq"
    > (now that I explained that I have custom error page redirection)
    > correctly redirects me to the correct "http://localhost/faq" page if
    > the url in my browser is not a subdirectory and redirects me to the
    > incorrect/inexistent "http://localhost/SUBDIR/faq" page if the url in
    > my browser contains a subdirectory.
    >
    > I included an example of a correct and incorrect scenario screenshots,
    > depending on the url in the browser to help better understand.
    >
    > http://img380.imageshack.us/img380/7554/tilt7om.gif
    >
    > Hope this is enoughe to solve my problem!
    >
    > Thanks a lot all for your time
    the4man, Feb 21, 2006
    #8
  9. ibiza

    whizsid

    Joined:
    Jul 3, 2008
    Messages:
    1
    Use
    ~/../default.aspx instead of ~/default.aspx.
    whizsid, Jul 3, 2008
    #9
    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. Jared Tullis
    Replies:
    11
    Views:
    3,129
    shekharshashi2000
    Mar 28, 2008
  2. Karo

    Tilde (~) in url

    Karo, Jul 5, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    7,066
    Oleg Ogurok
    Jul 5, 2005
  3. =?Utf-8?B?QWxm?=
    Replies:
    4
    Views:
    830
    =?Utf-8?B?QWxm?=
    Mar 14, 2007
  4. Replies:
    1
    Views:
    10,107
  5. Harsh Trivedi

    Tilde ~ causes problem - URL Rewrite

    Harsh Trivedi, Dec 26, 2007, in forum: ASP .Net
    Replies:
    4
    Views:
    531
    Guest
    Dec 27, 2007
Loading...

Share This Page