I have a class like below (simplified) and I like to replace the default validation error with my own messages. I know I can set the message directly when I am adding the attribute, but it is too much pain and it is error prune to add the same message 10,000 time in the same code.
Public class CreateUser
{
[Required(ErrorMessage = "A specific message")]//Don't like to do this
[MinLength(10)]// Like to write specific text
public string Name { get; set; }
}
I have looked into their code and apparently they are using ResourceManager to set the actual message from the key. The documentation just describes the way to handle windows applications. How do I add the resources in Asp.net core application and overwrite the default values?
Update
From the code it seems like a small change if one knows how to set the ResourceManager, but from the comments it seems like a hard job. I don't understand why. For MinLenghtAttribute we have
public class MinLengthAttribute : ValidationAttribute
{
public MinLengthAttribute(int length)
: base(SR.MinLengthAttribute_ValidationError)
{
Length = length;
}
// The rest of the code
}
The SR is as below :
namespace System
{
internal static partial class SR
{
internal static string MinLengthAttribute_ValidationError =>
GetResourceString("MinLengthAttribute_ValidationError");
// The rest of the code
}
}
One should be able to chance the value in GetResourceString and achieve my goal. Right?