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?
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.
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.
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.
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"].
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With