Is it possible to write a .NET API controller without using the ApiController attribute and without inheriting from ControllerBase or Controller?
The short answer is YES.
But, how will .NET identify this as a controller?
If a public class’s name ends with the word Controller, it will be considered a controller class, and the methods defined inside that class will be considered action methods. Generally, controllers inherit from ControllerBase or Controller as per convention.
The major drawback of not explicitly using the ApiController attribute is that functionalities provided by this attribute—such as model binding, model validation, error response handling, etc.—need to be handled manually.
Conclusion:
The best practice is to always follow the convention by using the ApiController attribute and inheriting from ControllerBase or Controller.
Top comments (0)