asp.net-mvc - MVC 中 ../path 和 ~/path 表示什么?

标签 asp.net-mvc razor path html-helper src

我有一个名为 Image 的自定义助手类,它有两个参数:

  • alt

  • 现在我要在其中一个 View 中调用它,
    @Html.Image("../Images/Indian.gif","Image is not supported or exist")
    <img src="~/Images/Indian.gif" alt="Image is not supported or exist" />
    

    现在,这两者都会给我相同的结果,但我很困惑,为什么两者的路径都不相同以及“../path”和“~/path”表示什么?

    当我在 Web 浏览器中检查元素时,它会生成以下两行:
    <img alt="Image is not supported or exist" src="../Images/Indian.gif" />
    <img src="/Images/Indian.gif" alt="Image is not supported or exist" />
    

    最佳答案

    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.



    刚用时../相对于 Web 服务器的常规路径。意味着从当前位置向上走一条路径(请记住:. = This location | .. = Up a directory)。
    ~字符提供虚拟路径并引用网站的根目录。

    关于asp.net-mvc - MVC 中 ../path 和 ~/path 表示什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43735333/

    相关文章:

    c# - 首先在 Entity Framework 数据库中级联删除

    c# - asp.net cron job quartz.net 错误,不总是触发

    .net - Razor 与电子邮件模板的查找和替换

    c++ - C++获取APPDATA的路径

    c# - 向 SQL Express 表添加全文搜索

    c# - 如何将 AngularJS 与 ASP.NET MVC 一起使用?

    asp.net-mvc - 有没有办法将 Html.BeginForm 集中设置为 autocomplete=off?

    asp.net-mvc-3 - 在 ASP MVC 3 中获取单选按钮值列表

    python - 将文件保存在特定目录中的常见路径名操作

    java - 寻找 DAG 中 2 个顶点之间的最短路径(未加权)