Repository navigation
Description
Describe the bug
CA1822 (Mark members as static) only skips externally visible members when the project is a web project, which CompilationExtensions.IsWebProject decides from the MSBuild property UsingMicrosoftNETSdkWeb (or legacy web
ProjectTypeGuids). It never checks whether a member is an ASP.NET Core controller action.
I have controllers are declared in a class library (Microsoft.NET.Sdk) that references a project that uses Microsoft.NET.Sdk.Web
so ASP.NET Core is available but not specified in this project. Hence, every public action that doesn't touch instance state is reported, and applying the code fix makes the action static, which MVC ignores.
| Project | SDK | UsingMicrosoftNETSdkWeb |
CA1822 on a stateless public action |
|---|---|---|---|
| Web host | Microsoft.NET.Sdk.Web |
true |
Not reported |
| Class library with the same controller | Microsoft.NET.Sdk |
not set | Reported |
Steps to reproduce
.editorconfig raises the rule to a warning:
[*.cs]
dotnet_diagnostic.CA1822.severity = warningControllerLibrary/ControllerLibrary.csproj:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
</Project>ControllerLibrary/ClockController.cs:
using Microsoft.AspNetCore.Mvc;
[ApiController]
[Route("api/clock")]
public class ClockController : ControllerBase
{
[HttpGet]
public string Get([FromServices] TimeProvider time) => time.GetUtcNow().ToString("O");
}Run dotnet build.
Expected behavior
No CA1822 for ClockController.Get: ASP.NET Core requires actions to be instance methods.
Actual behavior
ControllerLibrary\ClockController.cs(8,19): warning CA1822: Member 'Get' does not access instance data and can be marked as static
Are there any workarounds?
| Workaround | Drawback |
|---|---|
Set <UsingMicrosoftNETSdkWeb>true</UsingMicrosoftNETSdkWeb> in the library |
Misrepresents the project; skips all public members, not only actions. |
dotnet_diagnostic.CA1822.severity = none for controller folders, or dotnet_code_quality.CA1822.api_surface = private, internal |
Loses the rule for real candidates. |
[SuppressMessage] / #pragma per action |
Noise on every stateless action; easy to forget, and the fix is offered in the IDE. |
Proposal
CA1822 should skip members that ASP.NET Core would treat as controller actions, independent of the project SDK, by applying ASP.NET Core's own rules through WellKnownTypeProvider. The rule only applies when Microsoft.AspNetCore.Mvc.ControllerAttribute resolves in the compilation, so projects without ASP.NET Core MVC are unaffected. WellKnownTypeNames already defines MicrosoftAspNetCoreMvcControllerAttribute, MicrosoftAspNetCoreMvcNonControllerAttribute and MicrosoftAspNetCoreMvcNonActionAttribute.
Activity
github-actions commentedon Nov 18, 2026
🎯 Agentic Issue Triage
Triage Assessment:
@Evangelink| @dotnet/dotnet-analyzershigh- The issue directly concerns the CA1822 NetAnalyzers rule, which CODEOWNERS maps unambiguously.➡️ Summary: CA1822 flags stateless public ASP.NET Core controller actions in class libraries not using Microsoft.NET.Sdk.Web. Consider skipping members of MVC controllers when the controller attribute types resolve.
github-actions commentedon Nov 19, 2026
🍪 Issue Monster selected this for Copilot
I have identified this issue as a good candidate for automated resolution and requested assignment to the Copilot coding agent.
If assignment succeeds, the Copilot coding agent will analyze the issue and create a pull request with the fix.
Om nom nom! 🍪