shxo
shxo

Reputation: 179

ASP.NET ~/ not resolving to "/"

My application paths … <a runat="server" href="~/Home">

…are resolving to “Home” (not “/Home”). After I rewrite URLs, “/Blah/Blah”, all the ”~/” links are relative to the rewrite: /Blah/Home

Is there a way to force a root to “/”?

Upvotes: 3

Views: 437

Answers (4)

Brian Mains
Brian Mains

Reputation: 50728

~/ gets translated in the web controls, as in not Otherwise, you need to use ResolveClientUrl to do that.

For the hyperlink control, it will automatically map correctly for you.

Upvotes: 0

Residuum
Residuum

Reputation: 12064

If you use standard HTML tags like a, then include the url via

<a href="<%=ResolveUrl("~/Home")%>">...</a>

or use asp.net hyperlinks:

<asp:Hyperlink NavigateUrl="~/Home" runat="server" ID="HomeLink" Text="..." />

That way, all links will point to the right URL, even when the web application will be installed in a subdirectory.

<% %> is inline coding for asp.net, and <%= %> outputs the content, in that case the result of ResolveUrl.

Upvotes: 0

Rob
Rob

Reputation: 45789

If you're sure that, for the links in question you'll always be at a root of "/", then the simplest thing to do is change the <a> so that the href reads "/Home" rather than "~/Home" ... That way asp.net won't parse it and change it to use the App/VDir as its starting point.

Upvotes: 0

Dan McClain
Dan McClain

Reputation: 11920

Why don't you just write the links relative to the root ('/') instead of '~/', if you're application is not at the root of the domain, then the '~/' links will resolve to the root of the application

Upvotes: 1

Related Questions