Localize Minimal API validation messages in .NET 11
ASP.NET Core 11 adds built-in localization for validation messages and display names in Minimal APIs and Blazor. Registering AddValidation and AddLocalization, plus culture-specific resx files, is enough to return 400 responses in the caller's language. The author shows a working sample and an integration test across four cultures.
Key points
- Add AddLocalization and AddValidation in Program.cs; no extra package is required.
- The lookup key comes from ErrorMessage, or else from convention keys like Type_Member_Attribute_Error.
- Use [Display(Name = ...)] with a resource key to translate field names; the value feeds {0}.
- Make sure UseRequestLocalization runs, because resource lookup uses CurrentUICulture.
- Keep models in .cs files, since the validation source generator cannot see types declared in .razor files.
- In .NET 11, [ValidatableType] and [SkipValidation] are stable and need no ASP0029 suppression.
Localized validation messages have long been built into ASP.NET Core MVC, but Minimal APIs needed FluentValidation, a custom endpoint filter or hand-written localizer code. In .NET 11, the Microsoft.Extensions.Validation package brings this support to Minimal APIs and Blazor. Calling AddValidation enables automatic validation of supported parameters, and AddLocalization switches on localization inside that pipeline.
When ErrorMessage is set, its value acts as the resource key. Otherwise the framework tries convention keys such as Type_Member_Attribute_Error before falling back to the attribute's built-in English text. The culture comes from UseRequestLocalization, which reads the query string, a cookie or the Accept-Language header. The author also notes that models must live in .cs files, and that class libraries need their own AddValidation hook.
Real-world use
- A multi-language SaaS with a Minimal API customer signup endpoint that returns 400 errors in the caller's language.
- A Blazor team that wants form validation messages translated from shared resx files.
- A shared class library that exposes its own AddValidation extension so host apps can enable localized validation for its models.
Why it matters
With .NET 11, Minimal APIs can return localized validation errors without FluentValidation or custom filters, so hand-rolled localization code may be removable.