【问题标题】:How to access an Xml file from the Bin Directory for a Web API app?如何从 Web API 应用程序的 Bin 目录访问 Xml 文件?
【发布时间】:2014-08-04 07:30:13
【问题描述】:

我已经尝试了这些选项:

var resourcePath = "data.xml";
Application files are located in: c:/MyApp/bin/Release.
AppDomain.CurrentDomain.BaseDirectory refers to "c:/MyApp" folder.
AppDomain.CurrentDomain.SetupInformation.PrivateBinPath refers to "c:/MyApp/bin/Release" folder.
HttpContext.Current.Server.MapPath(resourcePath) refers to http://www.domain.com/MyApp/data.xml

只有 PrivateBinPath 有效,但根据定义,它可以包含多个 bin 目录,所以也许我不应该使用它。

Web API 应用程序访问 Bin 文件夹中文件的标准方法是什么?

【问题讨论】:

  • 标准规定数据文件在 APP_DATA 文件夹中,而不是在 BIN 文件夹中。你能解释一下为什么在那个文件夹中需要这个文件吗?
  • 是的,我同意,但这是一个带有 asp.net 3.5 的旧版应用程序。无论如何,您在 bin 文件夹中复制后如何访问该文件?
  • 你试过HttpContext.Current.Server.MapPath("~/bin", "data.xml")吗?
  • 非常感谢,是的工作:)

标签: c# asp.net asp.net-web-api appdomain httpcontext


【解决方案1】:

如果 BIN 文件夹是您的根站点的直接子文件夹,那么

string resourceFile = HttpContext.Current.Server.MapPath("~/bin", "data.xml");

应该为您提供所需的路径(如果您将应用程序移动到外部托管站点上也可以使用)。当然,作为 BIN 受保护的文件夹,您应该确保拥有在此路径中读取或写入文件所需的权限。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-07
    • 2011-01-21
    • 2012-10-18
    • 1970-01-01
    • 2012-02-07
    • 2016-06-18
    相关资源
    最近更新 更多