Browse pages
LS2003: WebApi Uses an MVC Controller
LS2003 is a compiler check that reports an MVC controller class in the WebApi project. A controller groups several HTTP operations behind shared filters and routing, while the .NET stack gives each operation its own endpoint class, so each route can be read and reviewed on its own. This page states what the rule reports, where the warning sits, and how to replace the controller with endpoints.
Reference#
What the rule enforces#
LS2003 enforces Exclude MVC controllers from the stack (litespec/backend/dotnet/api#exclude-mvc-controllers-from-the-stack). It runs in WebApi only. Its category is LiteSpec.Layers. It reports a warning and is enabled by default, as the Analyzers overview describes.
What the rule reports#
The rule reports a class that derives from ControllerBase or Controller, directly or through an intermediate base, or that carries the ApiController attribute. A class that does both gets one diagnostic. The diagnostic sits on the class name.
Failing example#
public sealed class OrdersController : ControllerBase { }Passing example#
internal sealed class GetOrderEndpoint : IEndpoint
{
public void Map() { }
}How to fix#
Map one IEndpoint per operation instead of routing through a controller.
Notes#
What the rule ignores#
The rule does not report endpoint classes. It does not run outside WebApi.