asp.net: refer to a page relative to root in normal non-server-side html controls
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 a开发者_运维问答ssume the root relative path will resolve for server controls
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>
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.
精彩评论