【问题标题】:how can an .ASPX page get its file system path?.ASPX 页面如何获取其文件系统路径?
【发布时间】:2008-10-09 17:02:38
【问题描述】:

我有一个页面 something.aspx,关联的代码隐藏在 something.aspx.cs。在那个代码隐藏中,我想知道 something.aspx 的文件系统位置。有什么方便的获取方式吗?

更新:我得到了几个很好的答案,不幸的是因为我正在做其他疯狂的事情而没有奏效。我在我传入的 URL 上编码了一些额外的信息,所以它看起来像这样:

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

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

【问题讨论】:

  • 在 MapPath 行设置断点,然后检查 Request.Url 的属性。你会在里面找到很多有用的东西。

标签: asp.net


【解决方案1】:
// 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】:

【讨论】:

  • 赢家! Request 对象组织得非常糟糕,很难记住所有不同的地方。
  • 成功了,谢谢!我最终得到的实际行是: string servicesPath = Path.GetDirectoryName(Request.PhysicalPath) + @"\services\";
【解决方案3】:

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

string physicalPath = Server.MapPath(Request.Url);

【讨论】:

    【解决方案4】:

    Server.MapPath( Request.AppRelativeCurrentExecutionFilePath )

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-02
      • 1970-01-01
      • 2015-11-16
      • 1970-01-01
      • 1970-01-01
      • 2013-08-05
      相关资源
      最近更新 更多