在 ASP.NET 中,当 HttpContext.Current 为 NULL 时如何获取物理文件路径?
我正在使用 DotNetNuke 的调度程序来安排任务,并且希望获取我创建的电子邮件模板的物理文件路径。 问题是HttpContext为NULL,因为计划任务在不同的线程上并且没有http请求。 您将如何获取文件的物理路径?
I'm working with DotNetNuke's scheduler to schedule tasks and I'm looking to get the physical file path of a email template that I created. The problem is that HttpContext is NULL because the scheduled task is on a different thread and there is not http request. How would you go about getting the file's physical path?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
System.Web.Hosting.HostingEnvironment.MapPath 就是您要寻找的。 每当您使用 Server 或 HttpContext.Current 对象时,请首先检查 HostingEnvironment 有您所需要的。
System.Web.Hosting.HostingEnvironment.MapPath is what you're looking for. Whenever you're using the Server or HttpContext.Current objects, check first to see if HostingEnvironment has what you need.
有很多方法可以做到这一点,我个人通过将路径信息存储为模块的配置选项来解决这个问题,它并不优雅,但它每次都有效。
Joe Brinkman 我相信附近有一篇博客文章介绍了如何构建一个新的 HTTPContext 以在调度程序中使用。
There are many ways of doing this, I personally get around it by storing path information as a config option for my modules, it isn't elegant, but it works and works every time.
Joe Brinkman I belive somewhere around has a blog posting on how to construct a new HTTPContext for use inside the scheduler.
由于此过程实际上是与网站相关的带外过程,因此也许您可以将路径放入配置文件中。
可能不是最好的主意,但它是一个替代方案。
Since this process is really out-of-band in relation to the web site, maybe you can just put the path in a config file.
May not be the best idea, but it is an alternative.
this.GetType().Assembly.Location 说什么?
what says this.GetType().Assembly.Location ?
你能看看大会和会议吗? 像这样的代码库路径:
这种东西并不总是有效,所以我建议做的是编写一个包含大量有关程序集的数据的日志,以查看该位置的工作原理。 当我创建要在 AppCenter 中托管的 COM 组件时,我必须这样做才能获得类似的东西。 我用它来“获取”“APP_BASE”应该是什么,并设置它,以便 app.config 文件能够正确加载。
Can you look at the Assembly & the CodeBase paths like this:
That kind of stuff doesn't always work, so what I would recommend doing is writing a log with a bunch of data about the assembly, to see what works in this location. It is what I had to do to get something similar when I was creating a COM component to be hosted in AppCenter. I used this to "get" what "APP_BASE" should be, and set that, so the app.config file would load properly.