SonarSource/sonar-dotnet

Improve AspNetMvcHelper.ReferencesControllers method: add parameter for .NET Core

zsolt-kolbay-sonarsource opened this issue · 1 comments

Currently the AspNetMvcHelper.ReferencesController() method returns true if the analyzed project references ASP.NET MVC Core or ASP.NET MVC (.NET Framework).
Several rules misuse this method to check whether the analyzer should be registered for the given project.
e.g. AnnotateApiActionsWithHttpVerb is only implemented for ASP.NET Core, but it uses this method to check if the analyzer should be used on the project or not. If the project uses ASP.NET MVC controllers under the old .NET Framework then it will still (needlessly) analyze it, but it won't report anything, because the rule only works with types from ASP.NET Core.

TODO

Add a bool parameter to the method: ReferencesController(bool onlyAspNetCore). When set to true it will only return true if the project references ASP.NET Core MVC Controllers. When the parameter is set to false, it will return true if the project references either ASP.NET Core MVC Controller or the classical .NET Framework MVC Controllers.
Then update all ASP.NET rules to use it correctly: if the rule is only implemented for ASP.NET Core then call ReferencesController(onlyAspNetCore: true)

Alternative proposal: having two distinct methods, one for core (e.g. referencesCoreControllers) and one for framework (e.g. referencesFrameworkControllers).
We would have very few rules targeting ASP.NET 4.x in addition to ASP.NET Core, so where something like if (referencesCoreControllers() && referencesFrameworkController()) would be required.
I think it would be more explicit and simpler to read than a flag passed as input parameter.