In ASP.NET Core, controllers usually inherit either from ControllerBase or Controller. If a controller does not use any
View-specific functionality, it is recommended to inherit from ControllerBase.
The ControllerBase class contains all the necessary functionality to handle API
requests and responses. The Controller class inherits from ControllerBase and adds support for Views, PartialViews and ViewComponents.
Inheriting from Controller when not using any View-specific functionality exposes unnecessary methods and can lead to confusion about
the intention of the class.
Furthermore, inheriting from Controller may come with a performance cost. Even though the controller might only deal with API
operations, the support for Views might introduce some overhead from the MVC framework during the request processing pipeline.
An issue is raised when:
[ApiController] attribute. Controller. [NonController] attribute.
Change the base type of the controller from Controller to ControllerBase.
[ApiController]
public class MyController : Controller // Noncompliant: Inherit from ControllerBase instead of Controller.
// ^^^^^^^^^^
{
// ..
}
[ApiController]
public class MyController : ControllerBase
{
// ..
}