liuhongbo
liuhongbo

Reputation: 2131

How to access Application Object in Asp.net MVC Web Api Controller

I know in Asp.net MVC controller, we can access the application like this:

HttpContext.Application["AppVar"]

But in the Web api controller, there is no HttpContext, how to access the application Object then?

Upvotes: 12

Views: 13205

Answers (2)

Hsin-Yu Chen
Hsin-Yu Chen

Reputation: 59

object context;
if (Request.Properties.TryGetValue("MS_HttpContext", out context))
{
    var application= ((HttpContextBase)context).Application; 
}

you can get HttpContext object from Request.Propertiies

Upvotes: 0

Jason Meckley
Jason Meckley

Reputation: 7591

the http context still exists and it's a core component of ASP.Net. how you gain access to it is the issue at hand.

HttpContext.Current.Application["AppVar"].

Upvotes: 16

Related Questions