代码之家  ›  专栏  ›  技术社区  ›  Anders

本地文件ASP的映射路径。NET/MVC

  •  5
  • Anders  · 技术社区  · 17 年前

    <table>
        <tr>
            <th></th>
            <th>
                Name
            </th>
            <th>
                Length
            </th>
            <th></th>
        </tr>
        <%
        var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "uploads");
        foreach (var file in Directory.GetFiles(path))
        {
            var item = new FileInfo(file);
        %>
        <tr>
            <td></td>
            <td>
                <%=Html.Encode(Path.GetFileName(item.Name))%>
            </td>
            <td>
                <%=Html.Encode(item.Length >= 1024 ? item.Length / 1024 + " kilobytes" : item.Length + " bytes")%>
            </td>
            <td>
                // This is the line in question. Does not work as-is.
                <a href="<%= item.FullName %>"><%= Html.Encode(Path.GetFileName(item.Name)) %></a>
            </td>
        </tr>
        <% } %>
    </table>
    

    3 回复  |  直到 17 年前
        1
  •  12
  •   Craig Stuntz    17 年前

    使用Url。内容,例如:

    <img src="<%= Url.Content("~/Content/UserImages/FileName.jpg") %>" />
    

    波浪号的意思是“我网站的根目录,无论它在哪里。”你不必把文件放在Content中;您可以将它们放在网站根目录下的任何位置。

        2
  •  7
  •   AnthonyWJones    17 年前

    是的,相当于 BaseDirectory 在ASP中。NET应用程序 HttpRuntime.AppDomainAppPath Server.MapPath 方法有用。您可以通过以下方式访问服务器方法 HttpContext.Current.Server .

    话虽如此,你确定你想在你的视野中看到这种代码吗。在我看来,要显示的值列表应该由控制器生成。

        3
  •  1
  •   AASoft    17 年前
    <a href="<%= Url.Content(System.Web.VirtualPathUtility.ToAppRelative("~/" + file.Substring(AppDomain.CurrentDomain.BaseDirectory.Length))) %></a>