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

六月ゝ 毕业季﹏ 提交于 2020-06-12 06:29:08

问题


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" />

回答1:


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.



来源:https://stackoverflow.com/questions/43735333/what-path-and-path-indicates-in-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!