3

I've a page that is deeply nested. And one that is in the root path. Inside of the deeply nested page I have an anchor tag which is supposed to navigate to another page (which is not in the root, but it is easy to specify a root relative path).

I've done the following trying to specify a root relative path:

<a href="~/home/main.aspx">Home</a> -> This one gives me a 404 error. It is not able to resolve ~ part to the root path.

The other option is to travel one directory up:

<a href="../../../home/main.aspx">Home</a> -> This is headache.

Then I tried this:

<a href="/home/main.aspx">Home</a> -> This gave me a 404 again. It simply removed what came after the localhost:<port_number>/ part and affixed it with /home/main.aspx.

What is the way to specify a root relative path here?

PS: I assume the root relative path will resolve for server controls

2 Answers 2

8

A tilde (~) is only recognized by the WebControl.ResolveUrl method, so you will have to invoke this method on the Page, which is a WebControl

<a href='<%=ResolveUrl("~/home/main.aspx") %>'>Home</a> 
Sign up to request clarification or add additional context in comments.

3 Comments

But I also kind of fixed this by making the Virtual Path property to /. It helped...
@deo - / and ~/ refer to 2 different paths - while they may appear the same in your development environment on your server they may be different. / is the absolute root of the server whereas ~/' is the absolute root of the current application. Do you see the difference? You usually only want to use ~/. Be sure that when you use /` that you understand where it will be pointing.
You can also set runat="server" on an HTML control and ASP.NET will convert the ~/ to the application root.
0

If you use the asp.net hyperlink control you will be able to use the '~'. If you don't want to use a servercontrol I think your stuck.

'/' will take you to the root of the site on a regular link but you have to check how you have the virtual directory is set up.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.