如何在没有 HttpContext 的情况下确定文件的物理路径?

发布于 2024-09-15 19:17:46 字数 153 浏览 3 评论 0原文

我有一些在 ASP.NET MVC Web 应用程序中没有 HttpContext 的情况下运行的进程。此过程需要能够确定应用程序的 Contents 目录的物理路径以读取/写入数据。但是,由于它没有 HttpContext,所以我无法使用 Server.MapPath 等花哨的东西。建议?

I have some processes that run without an HttpContext in an ASP.NET MVC web application. This process needs to be able to determine the physical path to the Contents directory of the application for reading/writing data. But, since it is without an HttpContext, I don't get to use fancy things like Server.MapPath and such. Suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苏璃陌 2024-09-22 19:17:46

在网站中,最好使用 HttpRuntime .AppDomainAppPath,因为在执行路径的某些时刻(即站点启动时),没有 HttpContext.Current 可用。

另请参阅 这篇文章

In a website, it is best to use HttpRuntime.AppDomainAppPath, because in certain moments of the execution path (i.e. when the site starts up), there's no HttpContext.Current available.

See also this post.

讽刺将军 2024-09-22 19:17:46

执行此操作的最佳方法是使用 AppDomain。 BaseDirectory 属性。只要您不摆弄自定义应用程序域,它就会指向您的根应用程序目录。换句话说;这两个字符串是相同的:

string mapUsingAppDomain = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Contents");
string mapUsingServer = HttpContext.Current.Server.MapPath("~/Contents");

The best way to do this is using the AppDomain.BaseDirectory property. As long as you don't fiddle with custom application domains, it will point to your root application directory. In other words; these two string would be the same:

string mapUsingAppDomain = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Contents");
string mapUsingServer = HttpContext.Current.Server.MapPath("~/Contents");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文