Making global resources 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; }

About these ads

12 Responses to “Making global resources public”

  1. robert Says:

    ok, what if there are neeed to add another language for example Resource.fr.resx, there will be problems? is it enough to also set the embedded for Resource.fr.resx?

  2. Steffen Höhle Says:

    Hi Robert,
    there shouldn’t be any problems with that.
    You have to configure the properties of every resource file you want to have public access to as described. If you then for example switch your Culture/UICulture setting the proper resource file will be picked.

    Regards,
    Steffen

  3. Koen Stroobants Says:

    Thanks for this !

    Very simple to do if you know what to do…

    This article helped me a lot !

  4. damian Says:

    great! thanks for this!

  5. kourosh Says:

    Thank you for very useful post.
    now I am getting this error, is it possible to get rid of this?
    CS0433: The type ‘Resources.YJnanResource’ exists in both ‘c:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root8e53665\b2a5260e\assembly\dl3\4d3546e9\840fd972_112ecc01\YJnanMVC.DLL’ and ‘c:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root8e53665\b2a5260e\App_GlobalResources.phdwmfco.dll’

    • ch Says:

      i got the same error.
      it’s occurs in cshtml: @Resources.Global.String1

      • Владек Says:

        Figured it out. Move those Resource files out of your fucking App_GlobalResources folder into something else. And don’t forget to set that CustomNamespace to Resources, or whatever it was before in ‘Resources.labels’…

  6. Gabriele Mezzetti Says:

    Hi, thank you for this smart tip. However I’ve a problem with the aspx files where I used the resource embedded, like:

    <dxe:ASPxLabel ID="lblTipoTabella" runat="server"
    Text="”
    meta:resourcekey=”lblTipoTabellaResource1″ >

    I’m frorced to copy the resource file resx into the folder of the published site, and I haven’t got any alert/tool if i forget to put or update the file.

    Any suggestions?

    Best regards,

    Gabriele.

  7. Gabriele Mezzetti Says:

    Sorry,

    in the attribute text there where aspx tag with the reference to the resource that were cut by your site and were like:
    Resources:bwnRes, resTipoTabella.

    Gabriele.

  8. Emily Says:

    Hi, Steffen, thanks for sharing the solution, it works for my Nunit tests. Thanks!

    • Steffen Höhle Says:

      Hi Emily,
      thanks for your comment.
      Glad I could help.

      Best regards

  9. Oana Says:

    When you get this message
    The type ‘Resources.Resources’ exists in both ‘c:\Users\oana\AppData\Local\Temp\Temporary ASP.NET Files\root\c258439e\4fed5ab7\assembly\dl3\30e3f93d\7e0f80a2_d8cbce01\Noc.Web.DLL’

    remove the Custom Tool Namespace and leave it blank and in the model add the following

    using App.App_GlobalResources;

    and you can use it in the model like this:

    [Display(Name = "DSPEMAIL", ResourceType = typeof(Resources))]
    [DataType(DataType.EmailAddress)]
    public string Email { get; set; }


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: