Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / February 2006

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
ibiza - 19 Feb 2006 00:27 GMT
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 - 19 Feb 2006 03:06 GMT
I think that "~" is not very useful.
Better use: "~/"
i.e. "~/default.aspx" instead of "~default.aspx"

> Hi all,
>
[quoted text clipped - 24 lines]
>
> ibiza
ibiza - 19 Feb 2006 03:31 GMT
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 - 19 Feb 2006 04:01 GMT
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?
Riki - 19 Feb 2006 14:29 GMT
> This is how I use it.
>
[quoted text clipped - 11 lines]
>
> 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)

Signature

Riki

Christopher Reed - 19 Feb 2006 14:38 GMT
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.
Signature

Christopher A. Reed
"The oxen are slow, but the earth is patient."

> 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)
ibiza - 19 Feb 2006 15:37 GMT
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 - 21 Feb 2006 22:01 GMT
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.
>
[quoted text clipped - 21 lines]
>
> Thanks a lot all for your time

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.