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

I

ibiza

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
 
L

Lloyd Dupont

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

ibiza

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?
 
I

ibiza

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?
 
R

Riki

ibiza said:
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)
 
C

Christopher Reed

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.
 
I

ibiza

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
 
T

the4man

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:
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top