I'm working with some WebForms/MVC-agnostic tools, and I need to get an instance of HttpContext
given a reference to an HttpContextBase
object. I can't use HttpContext.Current
because I need this to work asynchronously as well (HttpContext.Current
returns null
during an asynchronous request). I'm aware of HttpContextWrapper
, but goes the wrong way.
views:
557answers:
3
+3
A:
You can't.
The whole purpose of HttpContextBase is to abstract away the dependency on the concrete HttpContext class. While it may contain a concrete HttpContext (such as is the case with httpContextWrapper), other implementations may have absolutely nothing to do with HttpContext.
Your best option is to define a custom Abstract Factory that can get a HttContextBase for your, since you can always wrap a concrete HttpContext in a HttpContextWrapper.
Mark Seemann
2010-01-02 17:30:12
A:
Isn't HttpContextBase abstract class, meaning no one could create instance of it? My guess you just have to cast
Sergej Andrejev
2010-01-02 17:33:35
A:
You can,
var abstractContext = new System.Web.HttpContextWrapper(System.Web.HttpContext.Current);
Marc Chouteau
2010-08-12 21:07:10