Create rule S6961: API Controllers should derive from ControllerBase instead of Controller (#3846)

This commit is contained in:
github-actions[bot] 2024-04-19 08:59:27 +02:00 committed by GitHub
parent e752503a97
commit b91030a2d1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 112 additions and 0 deletions

View File

@ -0,0 +1,25 @@
{
"title": "API Controllers should derive from ControllerBase instead of Controller",
"type": "CODE_SMELL",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "5min"
},
"tags": [
"asp.net",
"performance"
],
"defaultSeverity": "Major",
"ruleSpecification": "RSPEC-6961",
"sqKey": "S6961",
"scope": "Main",
"defaultQualityProfiles": ["Sonar way"],
"quickfix": "targeted",
"code": {
"impacts": {
"MAINTAINABILITY": "MEDIUM"
},
"attribute": "COMPLETE"
}
}

View File

@ -0,0 +1,85 @@
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[]

View File

@ -0,0 +1,2 @@
{
}