Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET relative path

Tags:

path

asp.net

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

like image 446
Didier Levy Avatar asked Dec 31 '10 18:12

Didier Levy


People also ask

What is a relative path?

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.

What is relative and absolute path in C#?

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.


2 Answers

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.

like image 188
David Avatar answered Oct 14 '22 03:10

David


use this command

<a href="<%=Page.ResolveUrl("~/product.aspx")%>" >Link To Products</a>
like image 31
Mehdi Hamin Avatar answered Oct 14 '22 02:10

Mehdi Hamin