skip to content

In ASP.NET Core 10, how do you exempt a Minimal API endpoint from AddValidation, and where does the built-in validation filter run relative to your own endpoint filters?

level: seniorimportance: should knowfreq 30%

answer

  1. metadata checked when the endpoint is built
  2. DisableValidation() on an endpoint or group
  3. [SkipValidation] for one parameter or property
  4. inserted first, so outermost
  5. your filters never see invalid requests

basics

~20 s

Call .DisableValidation() on the endpoint or its group, or mark one parameter, property or type [SkipValidation]. The built-in filter is inserted ahead of all your filters, so a failed check returns 400 before any of them runs.

solid answer

~50 s

`DisableValidation()` is a convention that adds `IDisableValidationMetadata`; when an endpoint is built with that metadata, the framework does not add the validation filter at all. Because it is an ordinary convention, calling it on a route group exempts every endpoint in the group. For finer control, `[SkipValidation]` on a parameter, property or type stops validation there while the rest is checked; it is experimental in 10 (ASP0029). Placement: after the group and endpoint conventions have run, the framework inserts its validation filter factory at index 0 of the endpoint's filter list, making it the outermost filter. An invalid request therefore gets its 400 without any `AddEndpointFilter` filter running, group-level ones included, so an audit or timing filter never sees validation failures. Endpoints with nothing to validate get no filter layer, and DI-resolved parameters are never validated.

code

csharp · 13 lines
csharp
var bookings = app.MapGroup("/reservations")
    .AddEndpointFilter<AuditFilter>();

// Validated: the built-in filter wraps AuditFilter and the handler.
bookings.MapPost("/", (SeatReservation reservation) => TypedResults.Ok());

// Drafts may be incomplete: no validation filter is added to this endpoint.
bookings.MapPut("/drafts/{id:guid}", (Guid id, SeatReservation draft) => TypedResults.NoContent())
    .DisableValidation();

// Partner webhook payloads are checked elsewhere: opt out for the whole group.
app.MapGroup("/webhooks").DisableValidation()
   .MapPost("/partner-sync", (PartnerSync payload) => TypedResults.Accepted("/webhooks/partner-sync"));

go deeper

for a junior

Recall that .DisableValidation() exempts an endpoint and that the built-in check runs before your own endpoint filters.

for a middle

Explain how the opt-out metadata is read when the endpoint is built, how groups pass it on, what [SkipValidation] adds, and why the filter is outermost.

for a senior

Plan around the placement: audit and timing filters miss rejected requests, double validation must be switched off deliberately, and opt-outs belong on groups where a whole family of endpoints shares a reason.

for a principal

Keep opt-outs rare and visible: decide which endpoint families may skip framework validation, who owns the alternative checks, and how rejected-request telemetry is captured outside the filter chain.

## How the validation filter gets onto an endpoint When the routing data source builds a Minimal API endpoint, it applies group conventions, then the endpoint's own conventions, and then checks two things: 1. Are validation resolvers registered (`AddValidation()` was called)? 2. Does the endpoint's metadata contain `IDisableValidationMetadata`? If resolvers exist and the endpoint has not opted out, it **inserts the validation filter factory at position 0** of `FilterFactories`. The factory then runs once for that endpoint: it skips parameters that come from DI (marked `[FromServices]`, or of a type the container reports as a registered service), asks the resolvers about the rest, and returns `next` unchanged if nothing is validatable, which adds no per-request cost. ## Opting out | Tool | Scope | Status in 10 | |---|---|---| | `.DisableValidation()` on an endpoint | that endpoint: no filter is added | stable | | `.DisableValidation()` on a `RouteGroupBuilder` | every endpoint in the group, nested groups included | stable | | `[SkipValidation]` on a parameter | that argument only | experimental (ASP0029) | | `[SkipValidation]` on a property or type | that member, or every use of that type, nested members included | experimental (ASP0029) | Typical reasons to opt out in a rail-booking API: - **Draft endpoints** that save an incomplete reservation on purpose. - **Webhooks** whose payloads are validated by another mechanism, so the same body is not checked twice. - **Endpoints wired to FluentValidation** through their own filter, to avoid two competing error sets. ## Where it runs relative to your filters Because the factory is inserted at index 0 after all group and endpoint conventions, the order on a validated endpoint is: 1. the built-in validation filter; 2. filters from the outermost group, then inner groups; 3. the endpoint's own filters; 4. the handler. On failure the validation filter does not call `next`; it returns the 400 problem result. So: - an `AuditFilter` added with `AddEndpointFilter` on the `/reservations` group **never sees** rejected reservations; - a timing filter on the group records only requests that passed validation; - a custom filter can rely on the arguments having passed DataAnnotations checks. No `AddEndpointFilter` or `AddEndpointFilterFactory` call, on any group, can place a filter outside it. If rejected requests must be audited, observe them in middleware, read the response status after the endpoint runs, or customise the problem response through `IProblemDetailsService`. ## Choosing between the tools - Use `DisableValidation()` when the whole endpoint deliberately accepts input that would fail its model's rules. - Use `[SkipValidation]` when one argument or property must not be validated but the rest should be, accepting the experimental status in 10. - Do neither for performance alone: endpoints without validatable parameters already carry no validation layer. ## What interviewers listen for - `DisableValidation()` as endpoint metadata, and that it works on groups. - The filter's position: first in, outermost, short-circuiting before user filters. - That services are skipped and that a no-op endpoint pays nothing.

  • Does adding AddEndpointFilter<AuditFilter>() to the outermost group make it run before the validation filter?
    No. The validation filter factory is inserted at index 0 after every group and endpoint convention has been applied, so it always ends up outside user-added filters. Group placement only orders your filters among themselves; to observe rejected requests, use middleware or the problem-details customisation instead.
  • Will AddValidation try to validate an IBookingService parameter that has DataAnnotations on its implementation?
    No. The filter factory skips parameters that come from DI: those marked `[FromServices]`, and those whose type the container reports as a registered service through `IServiceProviderIsService`. Only bound request data is validated.

  • DisableValidation() works only on single endpoints, not on route groups.
  • ExcludeFromDescription() also switches off AddValidation for the endpoint.
  • Group filters run before the built-in validation filter because groups are outermost.
  • Every endpoint pays for the validation filter once AddValidation is called.
  • DisableValidation() removes [Required] checks from MVC controllers too.

Assumes ASP.NET Core 10: DisableValidation and the built-in validation filter are new in 10, and [SkipValidation] is experimental (ASP0029) in 10.