MVC6 and the IHttpContextAccessor

I've been working a lot with ASP.net 5 recently using the RC of visual studio 2015. There is considerable changes in both the .net framework and of course MVC.

One of the most significant changes is the dependency of APS.net 5 on the System.Web DLL. This DLL, that pretty much underpinned all that was web development, has been broken up into independent granular nuget packages that allow you to ship your application with exactly what you need.

Applications that we've used in the past have relied heavily on the HttpContext class allowing us to access the current context by the static property HttpContext.Current.

In ASP.net 5 large parts of the framework are now Dependency Inject-able. In order to access the current context you can simply inject the IHttpContextAccessor into your class in order to access the current context.


The beauty of this is we can then create our mocked HttpContext using the DefaultHttpContext object that can be found in Microsoft.AspNet.Http.Core.dll




Don't forget all .net code is now open sourced and this particular interface can be found at:

https://github.com/aspnet/Hosting/blob/release/src/Microsoft.AspNet.Hosting.Abstractions/IHttpContextAccessor.cs

The interface has also been moved to:

https://github.com/aspnet/HttpAbstractions/tree/release/src/Microsoft.AspNetCore.Http.Abstractions

Update:

As of RC2 some changes have been made to the IHttpContextAccessor and it's availability. The announcement on this change can be found here:

https://github.com/aspnet/Announcements/issues/190



3 comments:

  1. Oh thank you for the valuable information. What other software’s other than the visual basic and similar kinds are there to develop a versatile kind of software. There are somethings that need to be updated in the visual basics latest version.

    ReplyDelete
  2. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a .Net developer learn from .Net Core Training in Chennai. or learn thru .Net Core Training in Chennai. Nowadays Dot Net has tons of job opportunities on various vertical industry.
    or Es6 Training in Chennai. Nowadays JavaScript has tons of job opportunities on various vertical industry.

    ReplyDelete
  3. AWS Training in Bangalore - Live Online & Classroom
    myTectra Amazon Web Services (AWS) certification training helps you to gain real time hands on experience on AWS. myTectra offers AWS training in Bangalore using classroom and AWS Online Training globally. AWS Training at myTectra delivered by the experienced professional who has atleast 4 years of relavent AWS experince and overall 8-15 years of IT experience. myTectra Offers AWS Training since 2013 and retained the positions of Top AWS Training Company in Bangalore and India.


    IOT Training in Bangalore - Live Online & Classroom
    IOT Training course observes iot as the platform for networking of different devices on the internet and their inter related communication. Reading data through the sensors and processing it with applications sitting in the cloud and thereafter passing the processed data to generate different kind of output is the motive of the complete curricula. Students are made to understand the type of input devices and communications among the devices in a wireless media.

    ReplyDelete