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.

- 36,108
- 16
- 64
- 74

- 56,753
- 31
- 116
- 165
3 Answers
The simplest way is to get the application, ApplicationInstance
, and use its Context
property:
// httpContextBase is of type HttpContextBase
HttpContext context = httpContextBase.ApplicationInstance.Context;
(thanks to Ishmael Smyrnow who noted this in the comments)
Original answer:
You can, especially if the HttpContextBase
instance you've been handed is of type HttpContextWrapper
at run-time. The following example illustrates how you can do this. It supposes you have a method called Foo
that accepts context as HttpContextBase
but then needs to call a method in a third-party assembly (that you may not have the good fortune to modify) that is expecting the context to be typed as HttpContext
.
void Foo(HttpContextBase context)
{
var app = (HttpApplication) context.GetService(typeof(HttpApplication));
ThirdParty.Bar.Baz(app.Context);
}
// Somewhere in assembly and namespace ThirdParty,
// in a class called Bar, there is Baz expecting HttpContext:
static void Baz(HttpContext context) { /* ... */ }
HttpContextBase
has a method called GetService
as a result of supporting IServiceProvider
. The GetService
override of HttpContextWrapper
delegates to the GetService
implementation of the wrapped HttpContext
instance. The GetService
implementation of HttpContext
allows you to query for usual suspects like HttpApplication
, HttpRequest
, HttpResponse
and so on. It just so happens that HttpApplication
has a property called Context and which returns an instance of HttpContext
. So one gets at the wrapped HttpContext
instance by asking HttpContextBase
for HttpApplication
via GetService
followed by reading the Context
property of the returned HttpApplication
instance.
Unlike HttpContextBase
, GetService
does not appear as a public member of HttpContext
but that is because HttpContext
implements IServiceProvider.GetService
explicity while HttpContextBase
doesn't.
Bear in mind that Foo
is no longer testable because it relies on being able to unwrap the underlying HttpContext
during testing and which is next to impossible to fake/stub in the first place. The point of this answer, however, is to address the question, “How do I get an HttpContext object from HttpContextBase?”, literally. The illustrated technique is useful in those situations where you find yourself sandwiched between components you don't necessarily have the luxury to modify.
-
3Very interesting answer, just used it myself in MVC to pass the HttpContext from an Error Filter to ELMAH for logging. – Chris Marisic Feb 02 '11 at 16:41
-
1nice answer, bet that took a bit of digging? :) – longhairedsi Jun 17 '11 at 16:55
-
great answer! I utilized that especially in my Elmah to MVC controller https://github.com/alexanderbeletsky/elmah.mvc.controller/blob/master/Areas/Admin/Controllers/ElmahController.cs – Alexander Beletsky Aug 27 '11 at 15:49
-
38With an HttpContextBase, couldn't you call `context.ApplicationInstance.Context`? – Ishmael Smyrnow Oct 05 '11 at 21:24
-
@IshmaelSmyrnow Darn it, don't how I missed that among all the `Http*Base` variations returned by `HttpContextBase`. :P I'll update the answer. – Atif Aziz Dec 12 '11 at 11:06
You can,
var abstractContext = new System.Web.HttpContextWrapper(System.Web.HttpContext.Current);

- 6,219
- 8
- 38
- 73

- 447
- 4
- 2
-
18-1 this is the wrong way, this gives you the Base when you have the Context, not the other way around. – Chris Marisic Feb 02 '11 at 17:26
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 HttpContextBase
for you, since you can always wrap a concrete HttpContext
in a HttpContextWrapper
.

- 3,663
- 3
- 32
- 55

- 225,310
- 48
- 427
- 736
-
14This, by the way, was horribly done - I understand wanting to have a setup for unit tests, and I understand layers of abstraction - but WTH are you supposed to do when you want to use existing ASP.NET classes which expect an HttpContext? (like http://msdn.microsoft.com/en-us/library/system.web.sessionstate.sessionidmanager.aspx) – marq Jul 20 '11 at 01:00
-
Yep to test `IHttpHandler` implementations you end you having to put a sill number of abstractions in (Response wrappers) just for your unit tests, which doesn't feel right but ends up being the only option. – Chris S Jul 03 '13 at 17:13