Rediger

Del via


ASP0003: Do not use model binding attributes with route handlers

Value
Rule ID ASP0003
Category Usage
Fix is breaking or non-breaking Non-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:

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.

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.