Thursday, July 28, 2016

Making App_GlobalResources -> .RESX public


Regarding the previous post you 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: