如何在没有 HttpContext 的情况下确定文件的物理路径?
我有一些在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在网站中,最好使用
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 noHttpContext.Current
available.See also this post.
执行此操作的最佳方法是使用
AppDomain。 BaseDirectory
属性。只要您不摆弄自定义应用程序域,它就会指向您的根应用程序目录。换句话说;这两个字符串是相同的: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: