Search code examples
asp.net-mvcrazorpathhtml-helpersrc

What ../path and ~/path Indicates in MVC?


I have one custom helper class called Image which is having two arguments:

  1. src

  2. alt

Now I am going to call that in one of View,

@Html.Image("../Images/Indian.gif","Image is not supported or exist")
<img src="~/Images/Indian.gif" alt="Image is not supported or exist" />

Now, this both will give me same result but I am confused that why the path was not the same for both and what "../path" and "~/path" indicate?

This two-line it generates when I do inspect element in a web browser:

<img alt="Image is not supported or exist" src="../Images/Indian.gif" />
<img src="/Images/Indian.gif" alt="Image is not supported or exist" />

Solution

  • In ASP.NET, the tilde (~) refers to the application root directory. On the other hand, Two dots (..) refers to the folder that is one level higher than the current folder.

    When you just use ../ regular paths relative to the web server.Means go up a path from current location (Remember that : . = This location | .. = Up a directory).

    The ~ character provide virtual paths and refer to the root of the website.