Welcome to the navigation

Irure nisi sunt amet, adipisicing et qui lorem ad officia consectetur aliqua, ipsum mollit laborum, incididunt nostrud nulla consequat, exercitation laboris aliquip in dolore est. Est esse anim nulla deserunt mollit voluptate veniam, ad laborum, nostrud in ex laboris pariatur, ea commodo et cupidatat incididunt aute aliqua, in nisi velit

Yeah, this will be replaced... But please enjoy the search!

Get or construct a HttpContextWrapper inside a Web API Controller

The HttpContextWrapper is an essential part of the ASP.NET Web API since you will be able to not only use the common HttpContext data but also lots of other useful properties and methods. It is not in general recommended to use the HttpContextWrapper in testing and mocking, in such cases you should instad use the HttpContextBase from which the HttpContextWrapper inherits.

private HttpContextWrapper GetHttpContext(HttpRequestMessage request = null)
{
    request = request ?? Request;
 
    if (request.Properties.ContainsKey("MS_HttpContext"))
    {
        return ((HttpContextWrapper)request.Properties["MS_HttpContext"]);
    }
    else if (HttpContext.Current != null)
    {
        return new HttpContextWrapper(HttpContext.Current);
    }
    else
    {
        return null;
    }
}