Asp.net: link to page relative to root in regular html server-side controls

I have a page that is deeply nested. And one that is in the root path. Inside a deeply nested page, I have an anchored tag that should go to another page (which is not in the root, but it's easy to specify the root relative path).

I tried to specify the root path:

<a href="~/home/main.aspx">Home</a>→ This gives me error 404. It cannot resolve the part ~for the root path.

Another option is to move one directory up:

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

Then I tried this:

<a href="/home/main.aspx">Home</a>→ This again gave me 404. He simply deleted what appeared after the part localhost:<port_number>/and attached it to /home/main.aspx.

What is the way to indicate the root relative path here?

PS: I assume that the root relative path will be allowed to control the server

+3
source share
2 answers

Tilde (~) is recognized only by the method WebControl.ResolveUrl, so you have to call this method on Page, which isWebControl

<a href='<%=ResolveUrl("~/home/main.aspx") %>'>Home</a>
+6
source

If you use the asp.net hyperlink, you can use the "~". If you do not want to use servercontrol, I think you are stuck.

'/' will lead you to the root of the site using a regular link, but you should check how the virtual directory is configured.

0
source

Source: https://habr.com/ru/post/1755748/


All Articles