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).
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.
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.
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.
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.
Instance of ApiController is created new one on every HttpRequest.
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