Back to Aspnetcore

ASP0003: Do not use action results with route handlers

aspnetcore/diagnostics/asp0003.md

latest1.3 KB
Original Source

ASP0003: Do not use model binding attributes with route handlers

Value
Rule IDASP0003
CategoryUsage
Fix is breaking or non-breakingNon-breaking

Cause

A route handler delegate includes a parameter that has a [Bind] attribute.

Rule description

Route handler endpoints only support a subset of MVC attributes such as [FromRoute], [FromBody], etc. Unsupported attributes are ignored and result in unexpected binding behavior. For example, the following code results in an analyzer warning:

csharp
app.MapGet("/todos/{id}", ([Bind] int id) => new Todo { Id = id });

How to fix violations

To fix a violation of this rule, make sure that the endpoint uses one of the allowed model binding attributes.

csharp
app.MapGet("/todos/{id}", ([FromRoute] int id) => new Todo { Id = id });

When to suppress warnings

Do not suppress a warning from this rule. An incorrect model binding setup can result in unexpected behavior when resolving parameters at runtime.