.ASPX页面如何获取其文件系统路径?

Bru*_*uce 6 asp.net

我有一个页面something.aspx,相关的codebehind something.aspx.cs.在那个代码隐藏中,我想知道something.aspx的文件系统位置.有没有方便的方法来获得它?

更新:我得到了几个很好的答案,遗憾的是,由于我正在做的其他事情,这些答案无效.我正在编写一些关于我传入的URL的其他信息,所以它看起来像这样:

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

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

Dav*_*ult 10

// 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");
Run Code Online (Sandbox Code Playgroud)


Max*_*ler 5

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

string physicalPath = Server.MapPath(Request.Url);
Run Code Online (Sandbox Code Playgroud)