Problems with ResourceManager and neutral culture in Asp.Net MVC

I’ve been struggling with this for a couple of hours now and found a solution that I would like to share.

The problem is this: I added a resource file to my project as an Embedded Resource, configured the namespace as “Resources.Dialog” and set its Custom Tool property to “PublicResXFileCodeGenerator”. I then added some string resources to the file.

After inspection of the generated code file, everything looks normal. Next, I need to be able to access my resources dynamically, rather than simply using my Dialog resource class directly.

You would be tempted to do something like this:

ResourceManager dialogResources = new ResourceManager(typeof(Resources.Dialog)); string myString = dialogResources.GetString("MyString");  

This would work in theory, but instead (for me at least) it throws this nasty exception on the call to GetString():

MissingManifestResourceException: Could not find any resources appropriate for the specified culture or the neutral culture

I might be expecting this exception if I hadn’t provided a neutral culture resource file (which are basically your ‘fallback’ resources if you’re not using a supported culture), or if I had defined the resource file in a different assembly, but I’m not. This is a simple neutral culture resource file, in the same assembly as where the resource is being used from.

I don’t know why its doing this, but I do know what I changed to finally fix it.

The class which is generated by the resource file exposes a static ResourceManager instance which is set up to manage itself – so the solution is to use this rather than create your own!

Therefore, the above code becomes:

ResourceManager dialogResources = Resources.Dialog.ResourceManager; string myString = dialogResources.GetString("MyString");  

.. and the exception goes away.