@manhnguyenv

Welcome to my blog!

HttpContext vs HttpContextBase vs HttpContextWrapper

May 1, 2019 01:05

HttpContext vs HttpContextBase vs HttpContextWrapper (edit)

Http Context is handled a little differently in C# 3.5 onwards, its a little hard to get the full picture by looking at the msdn docs ("whats with these 2 new context classes???" is what i thought) so here's my take on it.

HttpContext

This is the vintage asp.net context. The problem with this is that it has no base class and isn't virtual, and hence is unusable for testing (cannot mock it). It's recommended to not pass it around as function arguments, instead pass around variables of type HttpContextBase.

HttpContextBase

This is the (new to c# 3.5) replacement to HttpContext. Since it is abstract, it is now mockable. The idea is that your functions that expect to be passed a context should expect to receive one of these. It is concretely implemented by HttpContextWrapper

HttpContextWrapper

Also new in C# 3.5 - this is the concrete implementation of HttpContextBase. To create one of these in a normal webpage, use new HttpContextWrapper(HttpContext.Current).

The idea is that to make your code unit-testable, you declare all your variables and function parameters to be of type HttpContextBase, and use an IOC framework eg Castle Windsor to get it injected. In normal code, castle is to inject the equivalent of 'new HttpContextWrapper(HttpContext.Current)', whereas in test code you're to be given a mock of HttpContextBase.

http://www.splinter.com.au/httpcontext-vs-httpcontextbase-vs-httpcontext/

Google Search: MVC 5

tip-of-the-day site:ittecture.wordpress.com

https://www.codingame.com/playgrounds/10771/asp-net-mvc-dos-and-dontsbest-practices

https://www.pluralsight.com/paths/mvc5 (HAY)

Integrate Google Oauth 2.O OpenId without Owin in sitecore asp.net mvc

http://santoshpoojari.blogspot.com/

Radio Button for Enum (C#)

https://stackoverflow.com/questions/5621013/pass-enum-to-html-radiobuttonfor-mvc3

UmbracoCMS

https://github.com/leekelleher

ParseQueryString in ASP.NET

https://leekelleher.com/2008/06/06/how-to-convert-namevaluecollection-to-a-query-string/

https://weblog.west-wind.com/posts/2014/Sep/08/A-NET-QueryString-and-Form-Data-Parser

https://stackoverflow.com/questions/68624/how-to-parse-a-query-string-into-a-namevaluecollection-in-net

http://burnignorance.com/asp-net-developer-tips/parse-query-string-using-httputility-parsequerystring/

https://www.hanselman.com/blog/ASPNETParamsCollectionVsQueryStringFormsVsRequestindexAndDoubleDecoding.aspx

http://densom.blogspot.com/2009/08/how-to-parse-query-string-without-using.html

http://www.codedigest.com/Articles/ASPNET/402_HttpUtility_Methods_in_AspNet.aspx

https://www.c-sharpcorner.com/blogs/add-remove-or-modify-the-query-string-value-in-url-in-asp-net1

http://www.dylanbeattie.net/2008/12/mocking-querystring-collection-in.html

https://www.ryadel.com/en/asp-net-c-sharp-parse-query-string-get-parameter-arrays-square-bracket-notation-http/ (HAY)

Google Search: MVC 6

https://code-maze.com/aspnetcore-webapi-best-practices/ (HAY)

Simple Asp.net Identity Core Without Entity Framework

https://code.msdn.microsoft.com/Simple-Aspnet-Identiy-Core-7475a961/view/Discussions

ASP.NET Web API Processing Architecture

https://blog.pedrofelix.org/2012/03/05/asp-net-web-api-processing-architecture/

Processing.Architecture

Categories

Recent posts