rlpwdg
Last Updated: February 25, 2016
·
6.69K
· artfulhacker
10425126 10103995008712411 6222461894198440691 n

How to get HttpContextBase from HttpContext.Current in the Global.asax?

In order to implement server-side google analytics tracking into our API for request monitoring we decided to give this library a try:

https://github.com/maartenba/GoogleAnalyticsTracker

In order to get this example below to work inside the Global.asax file

Tracker tracker = new Tracker("UA-XXXXXX-XX", "www.example.org");
tracker.TrackPageView(HttpContext, "My API - Create");

I needed to somehow cast HttpContext.Current into the abstract class HttpContextBase.

Turns out it is as simple as:

new HttpContextWrapper(HttpContext.Current)

so the example becomes:

Tracker tracker = new Tracker("UA-XXXXXX-XX", "www.example.org");
tracker.TrackPageView(new HttpContextWrapper(HttpContext.Current), "My API - Create");
Say Thanks
Respond