Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 441
liuhongbo Avatar asked May 31 '12 16:05

liuhongbo


People also ask

How can I get data from Web API in MVC controller?

First of all, create MVC controller class called StudentController in the Controllers folder as shown below. Right click on the Controllers folder > Add.. > select Controller.. Step 2: We need to access Web API in the Index() action method using HttpClient as shown below.

How is a controller used in an ASP NET core Web API application?

ApiController attributeThe [ApiController] attribute can be applied to a controller class to enable the following opinionated, API-specific behaviors: Attribute routing requirement. Automatic HTTP 400 responses. Binding source parameter inference.

Can I use MVC controller as Web API?

In order to add a Web API Controller you will need to Right Click the Controllers folder in the Solution Explorer and click on Add and then Controller. Now from the Add Scaffold window, choose the Web API 2 Controller – Empty option as shown below. Then give it a suitable name and click OK.


1 Answers

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"].
like image 76
Jason Meckley Avatar answered Sep 21 '22 07:09

Jason Meckley