Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is an ApiController instance created?

Is a new instance of an ApiController created to service every request, or does it get "pooled" in some way to service subsequent requests?

The reason I ask is that I'm wondering about the lifetime of the member variables such as the DbContext member that the scaffolding puts in (but also my own members).

like image 824
Gary McGill Avatar asked Nov 02 '12 13:11

Gary McGill


People also ask

What is an ApiController?

Web API Controller is similar to ASP.NET MVC controller. It handles incoming HTTP requests and send response back to the caller. Web API controller is a class which can be created under the Controllers folder or any other folder under your project's root folder.

What is the purpose of the ApiController attribute?

The [ApiController] attribute applies inference rules for the default data sources of action parameters. These rules save you from having to identify binding sources manually by applying attributes to the action parameters.

How do you call an ApiController?

If you want to change it for WebAPI you need to write a custom IHttpControllerTypeResolver . You only need to have a verbose route if you want your URL params to look nice. If not, api/{controller}/{action}/{id} with id optional is fine. Any additional parameters you send will just end up in the query string.

What is the difference between controller and ApiController?

They work similarly in Web API, but controllers in Web API derive from the ApiController class instead of Controller class. The first major difference you will notice is that actions on Web API controllers do not return views, they return data. ApiControllers are specialized in returning data.


1 Answers

Instance of ApiController is created new one on every HttpRequest.

like image 188
Kirill Bestemyanov Avatar answered Nov 16 '22 02:11

Kirill Bestemyanov