85 lines
3.9 KiB
Plaintext
85 lines
3.9 KiB
Plaintext
![]() |
In https://learn.microsoft.com/en-us/aspnet/core[ASP.NET Core], controllers usually inherit either from https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.controllerbase[`ControllerBase`] or https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.controller[`Controller`]. If a controller does not use any View-specific functionality, it is recommended to inherit from `ControllerBase`.
|
||
|
|
||
|
== Why is this an issue?
|
||
|
|
||
|
The `ControllerBase` class contains all the necessary functionality to handle https://en.wikipedia.org/wiki/Web_API[API] requests and responses. The `Controller` class inherits from `ControllerBase` and adds support for https://learn.microsoft.com/en-us/aspnet/core/mvc/views/overview[Views], https://learn.microsoft.com/en-us/aspnet/core/mvc/views/partial[PartialViews] and https://learn.microsoft.com/en-us/aspnet/core/mvc/views/view-components[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 https://learn.microsoft.com/en-us/aspnet/core/fundamentals/middleware[request processing pipeline].
|
||
|
|
||
|
An issue is raised when:
|
||
|
|
||
|
* The class is marked with the https://learn.microsoft.com/en-us/aspnet/core/web-api#apicontroller-attribute[`++[ApiController]++`] attribute.
|
||
|
* The class inherits _directly_ from `Controller`.
|
||
|
* No View-specific functionality is used in the class.
|
||
|
|
||
|
=== Exceptions
|
||
|
|
||
|
* If a class is marked with the https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.noncontrollerattribute[`++[NonController]++`] attribute.
|
||
|
* If a class does not have https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/public[public] accessibility.
|
||
|
|
||
|
|
||
|
== How to fix it
|
||
|
|
||
|
Change the base type of the controller from `Controller` to `ControllerBase`.
|
||
|
|
||
|
=== Code examples
|
||
|
|
||
|
==== Noncompliant code example
|
||
|
|
||
|
[source,csharp,diff-id=1,diff-type=noncompliant]
|
||
|
----
|
||
|
[ApiController]
|
||
|
public class MyController : Controller // Noncompliant: Inherit from ControllerBase instead of Controller.
|
||
|
// ^^^^^^^^^^
|
||
|
{
|
||
|
// ..
|
||
|
}
|
||
|
----
|
||
|
|
||
|
==== Compliant solution
|
||
|
|
||
|
[source,csharp,diff-id=1,diff-type=compliant]
|
||
|
----
|
||
|
[ApiController]
|
||
|
public class MyController : ControllerBase
|
||
|
{
|
||
|
// ..
|
||
|
}
|
||
|
----
|
||
|
|
||
|
== Resources
|
||
|
|
||
|
=== Documentation
|
||
|
|
||
|
* Wikipedia - https://en.wikipedia.org/wiki/Web_API[Web API]
|
||
|
* Microsoft Learn - https://learn.microsoft.com/en-us/aspnet/core[ASP.NET Core]
|
||
|
* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.controller[Controller Class]
|
||
|
* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.controllerbase[ControllerBase Class]
|
||
|
* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.apicontrollerattribute[ApiControllerAttribute Class]
|
||
|
* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.noncontrollerattribute[NonControllerAttribute Class]
|
||
|
* Microsoft Learn - https://learn.microsoft.com/en-us/aspnet/core/mvc/views/overview[Views in ASP.NET Core MVC]
|
||
|
* Microsoft Learn - https://learn.microsoft.com/en-us/aspnet/core/mvc/views/partial[Partial Views in ASP.NET Core MVC]
|
||
|
* Microsoft Learn - https://learn.microsoft.com/en-us/aspnet/core/mvc/views/view-components[View Components in ASP.NET Core MVC]
|
||
|
* Microsoft Learn - https://learn.microsoft.com/en-us/aspnet/core/fundamentals/middleware[ASP.NET Core Middleware]
|
||
|
|
||
|
ifdef::env-github,rspecator-view[]
|
||
|
|
||
|
'''
|
||
|
== Implementation Specification
|
||
|
(visible only on this page)
|
||
|
|
||
|
=== Message
|
||
|
|
||
|
* Inherit from ControllerBase instead of Controller.
|
||
|
|
||
|
=== Highlighting
|
||
|
|
||
|
The `Controller` base type of the class declaration.
|
||
|
|
||
|
'''
|
||
|
== Comments And Links
|
||
|
(visible only on this page)
|
||
|
|
||
|
endif::env-github,rspecator-view[]
|