ValidationLocalizationOptions.ErrorMessageKeyProvider Property
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Gets or sets the delegate that determines the localization lookup key for a
validation attribute's error message. When configured, the delegate is invoked for
every attribute and takes precedence over ErrorMessage.
Returning null or an empty string defers to using
ErrorMessage directly as the lookup key.
public:
property Func<Microsoft::Extensions::Validation::ErrorMessageLocalizationContext, System::String ^> ^ ErrorMessageKeyProvider { Func<Microsoft::Extensions::Validation::ErrorMessageLocalizationContext, System::String ^> ^ get(); void set(Func<Microsoft::Extensions::Validation::ErrorMessageLocalizationContext, System::String ^> ^ value); };
public Func<Microsoft.Extensions.Validation.ErrorMessageLocalizationContext,string?>? ErrorMessageKeyProvider { get; set; }
member this.ErrorMessageKeyProvider : Func<Microsoft.Extensions.Validation.ErrorMessageLocalizationContext, string> with get, set
Public Property ErrorMessageKeyProvider As Func(Of ErrorMessageLocalizationContext, String)
Property Value
Remarks
When null (the default), only attributes with ErrorMessage set are localized (using the ErrorMessage value as the key); attributes without an explicit ErrorMessage are not localized at all.
When configured, the delegate can:
- Provide a fallback key for attributes without an explicit ErrorMessage (convention-based key selection).
- Transform an existing ErrorMessage into a different lookup key by reading Attribute's ErrorMessage from the context.
- Return
nullor an empty string to defer to the default behavior of using ErrorMessage as the key.
options.ErrorMessageKeyProvider = context =>
$"{context.Attribute.GetType().Name}_ValidationError";
// This makes the localizer look up "RequiredAttribute_ValidationError"
// instead of "The {0} field is required."