Friday, July 20, 2012

Making global resources public

I am posting this here strictly as a reminder for myself but if anyone wants to comment about how bad a practice this is then please feel free. This is probably a better solution.

If you've ever asked this question then either read on or go get it from where I did. I lifted this from Holyhoehle's Blog and just wanted to be sure I could get the answer again in the future should his blog go offline.

[Y]ou may want to use strings from a global resources .resx file (App_GlobalResources) as error messages or descriptions in your attributes. This won’t work because global resource files are by default marked as internal and so you can’t access them from your controller or your model. Also the little “Access Modifier” dropdown menu in the resource editor is grayed out, so you can’t change the access level without further ado.
However there’s a way to change the access rights without editing the designer file by hand.
You just have to open the file properties of the resource file and change the “Custom Tool” from “GlobalResourceProxyGenerator” to “PublicResXFileCodeGenerator”, which is the default Tool for local resource files. Next you have to change the “Build Action” to “Embedded Resource”. You may also want to assign a proper Custom Tool Namespace like “Resources” in order to access the file properly, but this isn’t necessary.
Now rebuild the project and you should notice that the resource access is now public and you can access its contents from anywhere in your project.
E.g. like that:
[Required(ErrorMessageResourceType = typeof(Resources.Resource1), ErrorMessageResourceName = "Comment_Text")]
public string Myproperty { get; set; }

No comments:

Post a Comment

Mocking GCP APIs containing iterator structs

New company & language, who dis? The astute amongst my readers may have noticed that I left Lightbend to go work with some friends who ...