DisplayAttribute: No resources for resources in App_GlobalResources

This question applies to all projects, but my demo environment is an MVC3 application, therefore this tag.

I wrote the DisplayNameAttribute -derived class for our MVC2 projects, like so many others, so I could localize the display names in MVC editors and display templates. I raised the ValidationAttribute localization code used to report the error so that it behaves in a "standard" way.

Then I noticed that in MVC3 we have DisplayAttribute , so I dutifully deprecated my attribute class in the .NET version of my MVC extension infrastructure and changed the model properties in my current project to use this, for example:

 [Display(ResourceType = typeof(Resources.MyResources), Name = "ResourceName")] public string ModelProperty { get; set; } 

Then I launch the web application and I get an error similar to this:

System.InvalidOperationException: Unable to get the Name property because localization failed. The type '[Resource Type here]' is not public or does not contain a public property of a static string named "[Resource Name Here]".

I use localization in different places, of course, and in class libraries such resources will usually be marked as public. However, in web applications I will use the App_GlobalResources folder, because, well, what the App_GlobalResources !

The problem is that resource accessors created for resx files in this folder are always generated as internal access, without the possibility of changing them.

So, since the DisplayAttribute seems to only look for public members (whereas the localization code that I copied from ValidationAttribute and applied to my own type of DisplayNameAttribute is just looking for static members with open or internal visibility), it seems that DisplayAttribute not available for any web MVC applications that intend to use App_GlobalResources for localized strings?

Also, that this is probably a bug in DisplayAttribute , has anyone else got into this problem? What did you do to solve it? Do I have to abide by the use of DisplayAttribute and change all my resources as "correctly" embedded, as in class library projects, and set them as public? Or should I disable DisplayAttribute in favor of my other class, which I know works !?

+6
asp.net-mvc localization
source share
3 answers

You should be able to change the access modifier. In Solution Explorer, double-click the .resx file, then in the upper right corner of the Managed Resource Editor, change the Access Modifier drop-down list from Internal to Public .

Update

It might work: Publishing Global Resources

+9
source share

This post explains a bit more issues with using App_GlobalResources or App_Localresources in MVC applications.

It doesn't seem to make sense to use App_GlobalResources or App_Localresources in MVC applications, and it's best not to use them to avoid issues like the ones you are experiencing. (Another problem that you may encounter, according to the previous link, is the refusal of unit tests. This is because resources are only available when the ASP.NET runtime is running on your website)

+3
source share

You can create public details here.

0
source share

All Articles