Edit

ASP0034: Endpoint parameter type is inaccessible from generated code

Value
Rule ID ASP0034
Category Usage
Severity Warning

Cause

A Minimal API endpoint has a parameter whose type isn't accessible to the validation source generator. The type, or one of its containing types, is private or file-local.

Rule description

When AddValidation enables validation, the validation source generator discovers model types used by Minimal API endpoints. The generated code can't access private or file-local types, so validation is silently skipped for an inaccessible endpoint parameter type.

The following code produces this diagnostic:

app.MapPost("/orders", (Order order) => Results.Ok(order));

file class Order
{
    [Required]
    public string? CustomerName { get; set; }
}

How to fix violations

Make the parameter type and each of its containing types public or internal and don't declare them as file-local:

app.MapPost("/orders", (Order order) => Results.Ok(order));

internal class Order
{
    [Required]
    public string? CustomerName { get; set; }
}

If the endpoint shouldn't validate the parameter, disable validation for the endpoint or apply SkipValidationAttribute to the parameter.

For more information, see Validation in ASP.NET Core.