By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,846 Members | 1,252 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,846 IT Pros & Developers. It's quick & easy.

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

P: n/a
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

Feb 19 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
The tilde represents the root of the web application. So, if you make the
test subdirectory an application in IIS, then you can use the tilde for
http://localhost/test .
--
Christopher A. Reed
"The oxen are slow, but the earth is patient."

"ibiza" <la******@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
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

Feb 19 '06 #2

P: n/a
well 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 had 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 is
the same "~/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?

Feb 19 '06 #3

P: n/a
This is not incorrect. Your subdirectory test is also an application in
IIS, therefore it can also be represented by the tilde. It's all a matter of
perspective from IIS. If you call an ASPX page in the root directory, then
the application is http://localhost; however, if you call an ASPX page in
the test subweb, then the application is http://localhost/test. They are
treated by IIS as two separate applications. As such, the tilde will
represent two separate applications. Thus, when you call ~/faq.aspx from
the test application, it has to assume that the path is
http://localhost/test.
--
Christopher A. Reed
"The oxen are slow, but the earth is patient."

"ibiza" <la******@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
well 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 had 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 is
the same "~/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?

Feb 19 '06 #4

P: n/a
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 on the server! I used a custom error 404 page to redirect
correctly when a url is wrong (for example http://localhost/test, as it
cannot find the 'test' directory), 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 SUBDIR.

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

Feb 19 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.