开发者

ASP.NET ~/ not resolving to "/"

开发者 https://www.devze.com 2022-12-18 08:53 出处:网络
My application paths … <a runat=\"server\" href=\"~/H开发者_Python百科ome\"> …are resolving to “Home” (not “/Home”). After I rewrite URLs, “/Blah/Blah”, all the ”~/” links are relativ

My application paths … <a runat="server" href="~/H开发者_Python百科ome">

…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 “/”?


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


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.


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.


~/ 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号