In ASP.NET how you get the physcial file path when HttpContext.Current is NULL?
Asked Answered
D

5

8

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?

Diazo answered 20/9, 2008 at 20:29 Comment(0)
K
14

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.

Keto answered 22/9, 2008 at 13:53 Comment(2)
what if you're outside a web project but want to lets say get to the images folder of your web project to grab an image or to read lets say an .htm file's content that sites in your website but you have a utility method you want to call outside to get at that file?Antihelix
If you're outside of the web project, I would think that you'll need your own custom code to locate the web project anyway. Once you've got that, you can just keep following that path without any special helper like MapPath.Keto
D
1

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.

Dyeing answered 22/9, 2008 at 15:44 Comment(1)
For future readers, the Joe Brinkman articles are here, for the original version and here for an updated version.Immersionism
B
0

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.

Bannerman answered 20/9, 2008 at 20:33 Comment(0)
K
0

what says this.GetType().Assembly.Location ?

Knowlton answered 20/9, 2008 at 20:37 Comment(1)
It returns c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\mywebsite\50f1a686\ab5b581d\App_Code.xkcsxvg6.dllDiazo
A
0

Can you look at the Assembly & the CodeBase paths like this:

Imports System.Reflection
Imports System.IO
...
Path.GetDirectoryName( Assembly.GetExecutingAssembly().CodeBase ) 

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.

Log.Write ( Assembly.GetExecutingAssembly().CodeBase )
Log.Write ( Assembly.GetExecutingAssembly().Location )
Log.Write ( Path.GetFullPath(".") )
Log.Write ( Application.StartupPath )
... and so on, whatever you can think of ...
Atul answered 20/9, 2008 at 20:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.