Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 433
John Avatar asked Sep 20 '08 20:09

John


People also ask

Can HttpContext current request be null?

5 Answers. Show activity on this post. Clearly HttpContext. Current is not null only if you access it in a thread that handles incoming requests.

Where is HttpContext items stored?

It is stored in the memory of the server and the value is available for the entire lifetime of the request. Show activity on this post. I think the HTTPContext is stored on the server it comes from the HTTP request.

How does HttpContext current work?

From ASP.NET 4.5 and after, Current HttpContext is passed through CallContext instead of [ThreadStatic] , so context remains available through out async calls in single logical context instead of current thread as each async call may end up on different threads.

What is HttpContext current in C#?

The property stores the HttpContext instance that applies to the current request. The properties of this instance are the non-static properties of the HttpContext class. You can also use the Page. Context property to access the HttpContext object for the current HTTP request.


2 Answers

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.

like image 138
bdukes Avatar answered Nov 13 '22 11:11

bdukes


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.

like image 32
Mitchel Sellers Avatar answered Nov 13 '22 10:11

Mitchel Sellers