title: How do I set a default Resource .resx file for Data Annotations in ASP.NET Core 5.0 Web API?
body: <p>I'm referencing this MS Doc: <a href="https://docs.microsoft.com/en-us/aspnet/core/fundamentals/localization?view=aspnetcore-5.0#using-one-resource-string-for-multiple-classes" rel="nofollow noreferrer">https://docs.microsoft.com/en-us/aspnet/core/fundamentals/localization?view=aspnetcore-5.0#using-one-resource-string-for-multiple-classes</a>, which makes it look pretty simple.</p>
  <p>Currently, I have this:</p>
  <p>Startup.cs</p>
  <pre><code>services.AddControllers()
          .AddDataAnnotationsLocalization(options =&gt;
           {
               options.DataAnnotationLocalizerProvider = (type, factory) =&gt;
               {
                   return factory.Create(typeof(Validation));
               };
           });
  </code></pre>
  <p>The Validation.resx is located under the Resources folder and has a designer class.</p>
  <p>My model class:</p>
  <pre><code>public class MySearchRequest : BaseRequest
  {
      [Range(1, 5, ErrorMessageResourceName = &quot;MyExampleInfoId&quot;)]
      public long ExampleId { get; set; }
      [Required]
      public int SortOrder { get; set; }
      [Required]
      public int PageNumber { get; set; }
      [Required]
      public int PageSize { get; set; }
  }
  </code></pre>
  <p>Ideally, the Range attribute would populate with the message associated with the provided key from Validation.resx. However, this only works if I explicitly define ErrorMessageResourceType in the annotation. The exception that I see when I debug this behavior with a Custom Annotation is:</p>
  <blockquote>
  <p>System.InvalidOperationException: 'Both ErrorMessageResourceType and ErrorMessageResourceName need to be set on this attribute.'</p>
  </blockquote>
  <p>All validation annotations in my project use the same .resx and I'd like to not have to specify that in 500+ different annotations.</p>