1. <legend id='8vKBx'><style id='8vKBx'><dir id='8vKBx'><q id='8vKBx'></q></dir></style></legend>
  2. <i id='8vKBx'><tr id='8vKBx'><dt id='8vKBx'><q id='8vKBx'><span id='8vKBx'><b id='8vKBx'><form id='8vKBx'><ins id='8vKBx'></ins><ul id='8vKBx'></ul><sub id='8vKBx'></sub></form><legend id='8vKBx'></legend><bdo id='8vKBx'><pre id='8vKBx'><center id='8vKBx'></center></pre></bdo></b><th id='8vKBx'></th></span></q></dt></tr></i><div id='8vKBx'><tfoot id='8vKBx'></tfoot><dl id='8vKBx'><fieldset id='8vKBx'></fieldset></dl></div>

    <tfoot id='8vKBx'></tfoot>

      • <bdo id='8vKBx'></bdo><ul id='8vKBx'></ul>

      <small id='8vKBx'></small><noframes id='8vKBx'>

    1. 如何为验证属性提供本地化验证消息

      How to provide localized validation messages for validation attributes(如何为验证属性提供本地化验证消息)
        <bdo id='WR6IM'></bdo><ul id='WR6IM'></ul>
          <tbody id='WR6IM'></tbody>

        <i id='WR6IM'><tr id='WR6IM'><dt id='WR6IM'><q id='WR6IM'><span id='WR6IM'><b id='WR6IM'><form id='WR6IM'><ins id='WR6IM'></ins><ul id='WR6IM'></ul><sub id='WR6IM'></sub></form><legend id='WR6IM'></legend><bdo id='WR6IM'><pre id='WR6IM'><center id='WR6IM'></center></pre></bdo></b><th id='WR6IM'></th></span></q></dt></tr></i><div id='WR6IM'><tfoot id='WR6IM'></tfoot><dl id='WR6IM'><fieldset id='WR6IM'></fieldset></dl></div>

            <tfoot id='WR6IM'></tfoot>

            <small id='WR6IM'></small><noframes id='WR6IM'>

              1. <legend id='WR6IM'><style id='WR6IM'><dir id='WR6IM'><q id='WR6IM'></q></dir></style></legend>
                本文介绍了如何为验证属性提供本地化验证消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在开发一个

                IValidatioMetadaProvider 示例:

                使用 Microsoft.AspNetCore.Mvc.ModelBinding.Metadata;使用 System.ComponentModel.DataAnnotations;使用 System.Reflection;公共类 LocalizedValidationMetadataProvider : IValidationMetadataProvider{公共 LocalizedValidationMetadataProvider(){}公共无效 CreateValidationMetadata(ValidationMetadataProviderContext 上下文){if (context.Key.ModelType.GetTypeInfo().IsValueType && Nullable.GetUnderlyingType(context.Key.ModelType.GetTypeInfo()) == null && context.ValidationMetadata.ValidatorMetadata.Where(m =>m.GetType() == typeof(RequiredAttribute)).Count() == 0)context.ValidationMetadata.ValidatorMetadata.Add(newRequiredAttribute());foreach(上下文中的 var 属性.ValidationMetadata.ValidatorMetadata){var tAttr = 属性作为 ValidationAttribute;if (tAttr?.ErrorMessage == null && tAttr?.ErrorMessageResourceName == null){var name = tAttr.GetType().Name;if (Resources.ValidationsMessages.ResourceManager.GetString(name) != null){tAttr.ErrorMessageResourceType = typeof(Resources.ValidationsMessages);tAttr.ErrorMessageResourceName = 名称;tAttr.ErrorMessage = null;}}}}}

                将提供程序添加到 Startup 类的 ConfigureServices 方法中:

                services.AddMvc(options =>{options.ModelMetadataDetailsProviders.Add(new LocalizedValidationMetadataProvider());})

                I am working on an ASP.NET Core application and I would like to override the default validation error messages for data-annotations, like Required, MinLength, MaxLength, etc. I read the documentation at Globalization and localization in ASP.NET Core, and it seems that it does not cover what I was looking for...

                For instance, a validation error message for the Required attribute can always be the same for any model property. The default text just states: The {0} field is required, whereby the {0} placeholder will be filled up with the property’s display name.

                In my view models, I use the Required attribute without any named arguments, like this...

                class ViewModel
                {
                    [Required, MinLength(10)]
                    public string RequiredProperty { get; set; }
                }
                

                Setting an ErrorMessage or ErrorMessageResourceName (and ErrorMessageResourceType) is unnecessary overhead, in my opinion. I thought I could implement something similar to IDisplayMetadataProvider allowing me to return error messages for applied attributes, in case the validation has failed. Is this possible?

                解决方案

                For those that end up here, in search of a general solution, the best way to solve it is using a Validation Metadata Provider. I based my solution on this article: AspNetCore MVC Error Message, I usted the .net framework style localization, and simplified it to use the designed provider.

                1. Add a Resource file for example ValidationsMessages.resx to your project, and set the Access Modifier as Internal or Public, so that the code behind is generated, that will provide you with the ResourceManager static instance.
                2. Add a custom localization for each language ValidationsMessages.es.resx. Remember NOT to set Access Modifier for this files, the code is created on step 1.
                3. Add an implementation of IValidationMetadataProvider
                4. Add the localizations based on the Attributes Type Name like "RequiredAtrribute".
                5. Setup your app on the Startup file.

                Sample ValidationsMessages.es.resx

                Sample for IValidatioMetadaProvider:

                using Microsoft.AspNetCore.Mvc.ModelBinding.Metadata;
                using System.ComponentModel.DataAnnotations;
                using System.Reflection;
                
                public class LocalizedValidationMetadataProvider : IValidationMetadataProvider
                {
                    public LocalizedValidationMetadataProvider()
                    {
                    }
                
                    public void CreateValidationMetadata(ValidationMetadataProviderContext context)
                    {
                        if (context.Key.ModelType.GetTypeInfo().IsValueType && Nullable.GetUnderlyingType(context.Key.ModelType.GetTypeInfo()) == null && context.ValidationMetadata.ValidatorMetadata.Where(m => m.GetType() == typeof(RequiredAttribute)).Count() == 0)
                            context.ValidationMetadata.ValidatorMetadata.Add(new RequiredAttribute());
                        foreach (var attribute in context.ValidationMetadata.ValidatorMetadata)
                        {
                            var tAttr = attribute as ValidationAttribute;
                            if (tAttr?.ErrorMessage == null && tAttr?.ErrorMessageResourceName == null)
                            {
                                var name = tAttr.GetType().Name;
                                if (Resources.ValidationsMessages.ResourceManager.GetString(name) != null)
                                {
                                    tAttr.ErrorMessageResourceType = typeof(Resources.ValidationsMessages);
                                    tAttr.ErrorMessageResourceName = name;
                                    tAttr.ErrorMessage = null;
                                }
                            }
                        }
                    }
                }
                

                Add the provider to the ConfigureServices method on the Startup class:

                services.AddMvc(options =>
                {
                     options.ModelMetadataDetailsProviders.Add(new LocalizedValidationMetadataProvider());
                })
                

                这篇关于如何为验证属性提供本地化验证消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                How to keep the Text of a Read only TextBox after PostBack()?(PostBack()之后如何保留只读文本框的文本?)
                Winforms Textbox - Using Ctrl-Backspace to Delete Whole Word(Winforms 文本框 - 使用 Ctrl-Backspace 删除整个单词)
                C# - Add button click events using code(C# - 使用代码添加按钮单击事件)
                Multi-color TextBox C#(多色文本框 C#)
                How can i set the caret position to a specific index in passwordbox in WPF(如何将插入符号位置设置为 WPF 密码框中的特定索引)
                C# Numeric Only TextBox Control(C# 纯数字文本框控件)

                  <tbody id='yJEpE'></tbody>
                <tfoot id='yJEpE'></tfoot>

              2. <i id='yJEpE'><tr id='yJEpE'><dt id='yJEpE'><q id='yJEpE'><span id='yJEpE'><b id='yJEpE'><form id='yJEpE'><ins id='yJEpE'></ins><ul id='yJEpE'></ul><sub id='yJEpE'></sub></form><legend id='yJEpE'></legend><bdo id='yJEpE'><pre id='yJEpE'><center id='yJEpE'></center></pre></bdo></b><th id='yJEpE'></th></span></q></dt></tr></i><div id='yJEpE'><tfoot id='yJEpE'></tfoot><dl id='yJEpE'><fieldset id='yJEpE'></fieldset></dl></div>
                <legend id='yJEpE'><style id='yJEpE'><dir id='yJEpE'><q id='yJEpE'></q></dir></style></legend>

                      • <small id='yJEpE'></small><noframes id='yJEpE'>

                          <bdo id='yJEpE'></bdo><ul id='yJEpE'></ul>