21

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

3 Answers 3

35

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Or: <a href="<%= ResolveClientUrl("~/Account/Login.aspx") %>">Login</a> should give you the same result.
Yup, that'll do it too. As would using the <asp:Hyperlink> control. There are a number of different approaches, depending on what the developer wants to use.
Thanks David & Jamie. Happy New-Year to you both.
hi, is there any other way to perform this navigation. I have tried <asp:HyperLink ID="HyperLink1" NavigateUrl="~/Setup/Company.aspx" runat="server">HyperLink</asp:HyperLink> <a runat="server" href="~/Setup/Company.aspx">Company</a> <li><a runat="server" href="~/Setup/Company.aspx"><i class="fa fa-cloud"></i>Company</a></li> all three navigation paths are showing me the same error Cannot use a leading .. to exit above the top directory. my masterpage is on the root.
@MuhammadAli: Please use the "Ask Question" button on this page to ask a new question on Stack Overflow. Be sure to provide any relevant information about the problem in the question. Comment threads on a different existing question are not a good place to seek help from the community.
7

use this command

<a href="<%=Page.ResolveUrl("~/product.aspx")%>" >Link To Products</a>

Comments

0

You can use ~ when refering to URLs inside ASP.NET Server Controls. You are using it in a <a> tag which is just plain html that doeesn't know anything about ~ . use '"/Images/SampleImage.jpg"' instead

3 Comments

What I don't like about using the root "/" in a page is that it may be different depending on your environment. For example, if your app is in a virtual directory in XP in your dev environment, and at the web root on the production server in Server03, then you'd get different results.
"/Images/SampleImage.jpg" isn't a relative path, however. This approach will only work if the application's base path corresponds with the server root. While that's often the case, it's not guaranteed, and in point of fact not what was asked.
You are right. That was my easy answer though. Someone else has posted ResolveClientUrl which will work better.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.