Skip to content

Parameter validation in minimal API not working as expected #61995

@mikekistler

Description

@mikekistler

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

Certain validations, such as [Required] and [MinLength], are not working as expected on query or path parameters to a minimal API endpoint.

For example, this endpoint:

app.MapGet("/required-param", (
    [Required] string? name
) =>
{
    return TypedResults.Ok($"Hello {name}");
});

does not validate that a name was specified in the query string. Also, this endpoint

app.MapGet("/minlength-param", (
    [MaxLength(5)] string? name
) =>
{
    return TypedResults.Ok($"Hello {name}");
});

does not generate a validation error for a name with less that 5 characters.

The problem also occurs for path parameters, such as in this case:

app.MapGet("/path-param/{name}", (
    [MaxLength(5)] string? name
) =>
{
    return TypedResults.Ok($"Hello {name}");
});

Expected Behavior

Validations should be performed as specified.

Steps To Reproduce

Repro of all the cases above are in the project in the validate-required-params directory of this repo:

https://github.com/mikekistler/dotnet10-issue-repros

Exceptions (if any)

No response

.NET Version

10.0.100-preview.4.25258.110

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etcfeature-validationIssues related to model validation in minimal and controller-based APIs

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions