Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get an HttpContext object from HttpContextBase in ASP.NET MVC 1?

People also ask

How is HttpContext created?

When an HTTP request arrives at the server, the server processes the request and builds an HttpContext object. This object represents the request which your application code can use to create the response.

What is HttpContext MVC?

HttpContext is a type which has a static Current property that you're using to get the current context. There isn't a System. Web. Mvc.

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.


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.


You can,

var abstractContext = new System.Web.HttpContextWrapper(System.Web.HttpContext.Current);

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.