I'm confused with ASP.NET relative path, please can someone help?
In a Master Page I gave a link label referencing:
<a href="~/Account/Login.aspx">Login</a>
From the ASP.NET official documentation I read:
The following example shows the ~ operator used to specify a root-relative path for an image when using the Image server control In this example, the image file is read from the Images folder that is located directly under the root of the Web application, regardless of where in the Web site the page is located.
<asp:image runat="server" id="Image1"
ImageUrl="~/Images/SampleImage.jpg" />
With the Login markup, when I click the link from a page in the /Account folder, I'm redirected to:
/Account/~/Account/Login.aspx
Why? WHY?h
A relative path refers to a location that is relative to a current directory. Relative paths make use of two special symbols, a dot (.) and a double-dot (..), which translate into the current directory and the parent directory. Double dots are used for moving up in the hierarchy.
A relative file path is going to be a structure based around a root node; and an absolute path is going to be a structure based on a non ambiguous location.
Because you're using it directly in markup, rather than in a server control. Something as simple as this should fix it:
<a runat="server" href="~/Account/Login.aspx">Login</a>
Basically, the ~
path reference needs to be translated on the server, since it's a reference to the server path of the application's base directory. Plain HTML markup isn't processed on the server, it's just delivered as-is to the client. Only server-processed code will translate the ~
path to what it resolves to.
use this command
<a href="<%=Page.ResolveUrl("~/product.aspx")%>" >Link To Products</a>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With