Skip to content

CA1822 flags ASP.NET Core controller actions in projects that do not use Microsoft.NET.Sdk.Web #57207

Open

Description

@jeremy-morren

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 = warning

ControllerLibrary/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

  1. added
    untriaged
    on Nov 18, 2026
  2. locked and limited conversation to collaborators on Nov 18, 2026
  3. unlocked this conversation on Nov 18, 2026
  4. added
    cookie
    and removed
    untriaged
    on Nov 18, 2026
  5. github-actions commented on Nov 18, 2026

    @github-actions
    Contributor

    🎯 Agentic Issue Triage

    Triage Assessment:

    ➡️ 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.

    Generated by 🏷️ Issue Triage for #57207 · copilot · auto (Claude Sonnet 5.5) · ~16 AIC · ⌖ ~2 AIC · ⊞ 15.5K · ◷

  6. github-actions commented on Nov 19, 2026

    @github-actions
    Contributor

    🍪 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! 🍪

    🍪 Om nom nom by Issue Monster Orchestrator · copilot · auto (Claude Sonnet 5.5) · ~15 AIC · ⌖ ~25 AIC · ⊞ 13.6K · ◷

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions