HttpContext vs HttpContextBase vs HttpContextWrapper

HttpContext

Good ol’ HttpContext in ASP.Net. HttpContext has no base class and isn’t virtual, thus, is unusable for testing and unmockable. Strongly recommended to not pass it around as arguments. Instead, pass around variables of type HttpContextBase.

HttpContextBase

New to C# 3.5 and meant to replace HttpContext. HttpContextBase is abstract, thus, it is now mockable. Concretely implemented by HttpContextWrapper.

HttpContextWrapper

Also new in C# 3.5 – this is the concrete implementation of HttpContextBase. To instantiate in a normal webpage:

var webContext = new HttpContextWrapper(HttpContext.Current)

The whole idea is to make your code test-able by allowing your test framework to mock the HttpContext.

More detail explanation on here.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s