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

怎么可能。ASPX页面获取其文件系统路径?

  •  6
  • Bruce  · 技术社区  · 17 年前

    我有一个页面something.aspx,后面有相关的代码something.aspx.cs。在代码后面,我想知道something.aspx的文件系统位置。有什么方便的办法得到它吗?

    更新:我得到了几个很好的答案,但不幸的是,由于我正在做的其他疯狂的事情,这些答案都不起作用。我在传入的URL上编码了一些附加信息,所以看起来像这样:

    http://server/path/something.aspx/info1/info2/info3.xml

    服务器处理这个OK(我没有使用查询字符串参数来处理我没有编写的其他代码)。但当我呼叫服务器时。MapPath(Request.Url.ToString())我收到一个错误,带有“info”段的完整Url不是有效的虚拟路径。

    4 回复  |  直到 17 年前
        1
  •  10
  •   David Thibault    17 年前
    // File path
    string absoluteSystemPath = Server.MapPath("~/relative/path.aspx");
    // Directory path
    string dir = System.IO.Path.GetDirectoryName(absoluteSystemPath);
    // Or simply
    string dir2 = Server.MapPath("~/relative");
    
        2
  •  8
  •   Jason DeFontes    17 年前
        3
  •  5
  •   Maxime Rouiller    17 年前

    Server.MapPath 是最常用的方法之一。

    string physicalPath = Server.MapPath(Request.Url);
    
        4
  •  0
  •   Mark Cidade    17 年前

    Server.MapPath( Request.AppRelativeCurrentExecutionFilePath )

    推荐文章