Localization of MVC Error Messages

I want to localize my error messages in my model using attributes such as RequiredAttribute and RangeAttribute. I am using ASP.NET MVC2 in Visual Studio 2010.

It is really easy if my localized resources were in resx resource files ... using ErrorMessageResourceName and ErrorMessageResourceType ... however I need to integrate it with the user localization infrastructure.

I set resourceProviderFactoryType in the globalization section in web.config, but MVC does not use this. It tries to access a static property in a resource file that returns a localized string.

This is a call stack ...

System.ComponentModel.DataAnnotations.ValidationAttribute.SetResourceAccessorByPropertyLookup() +56576 System.ComponentModel.DataAnnotations.ValidationAttribute.SetupResourceAccessor() +146 System.ComponentModel.DataAnnotations.ValidationAttribute.get_ErrorMessageString() +12 System.ComponentModel.DataAnnotations.ValidationAttribute.FormatErrorMessage(String name) +33 System.Web.Mvc.DataAnnotationsModelValidator.get_ErrorMessage() +31 System.Web.Mvc.RequiredAttributeAdapter.GetClientValidationRules() +46 System.Web.Mvc.Html.ValidationExtensions.<ApplyFieldValidationMetadata>b__0(ModelValidator v) +10 System.Linq.<SelectManyIterator>d__14`2.MoveNext() +238 System.Web.Mvc.Html.ValidationExtensions.ApplyFieldValidationMetadata(HtmlHelper htmlHelper, ModelMetadata modelMetadata, String modelName) +207 System.Web.Mvc.Html.ValidationExtensions.ValidationMessageHelper(HtmlHelper htmlHelper, ModelMetadata modelMetadata, String expression, String validationMessage, IDictionary`2 htmlAttributes) +527 System.Web.Mvc.Html.ValidationExtensions.ValidationMessageFor(HtmlHelper`1 htmlHelper, Expression`1 expression, String validationMessage, IDictionary`2 htmlAttributes) +82 System.Web.Mvc.Html.ValidationExtensions.ValidationMessageFor(HtmlHelper`1 htmlHelper, Expression`1 expression) +75 

Does anyone know if it is possible for me to somehow override how MVC retrieves an error message for display?

thanks to

+4
source share
2 answers

I would create a custom class attribute that inherits from the standard Mvc class attribute HandleError ...

http://blog.dantup.com/2009/04/aspnet-mvc-handleerror-attribute-custom.html

+2
source

Source: https://habr.com/ru/post/1306837/


All Articles