2

I have a problem with GlobalResources

  1. I create App_GlobalResources folder
  2. Add User.resx
  3. Add Name = "FirstName", Value = "First name"

Default I can't use this resources in my MVC project. I tried:

  • App_GlobalResources.User.FirstName - did not work
  • Resources.User.FirstName - it works but only codebehind, when build and start application (local), show error:

Nie można pobrać właściwości „Name”, ponieważ lokalizacja nie powiodła się. Typ „Resources.Users” nie jest publiczny lub nie zawiera publicznej, statycznej właściwości ciągu o nazwie „FirstName”.

Which its translation is:

Unable to retrieve the properties "Name" as the location failed. Type "Resources.Users" is not a public or does not contain a public static property within named "FirstName."

Then I change User.resx properties:

  • Build Action: Embedded Resource
  • Copy to output: Directory Do not copy
  • Custom Tool: PublicResourceProxyGenerator
  • Custom Tool Namespace: "empty"

Now I can't use Resources.User.FirstName I have to useApp_GlobalResources.User.FirstName

I'm happy it's work. But yesterday is my first publish on the test server and resourses not working...

File does't copy to server...

I change User.resx properties

  • Build Action: Content
  • Copy to output: Directory Do not copy
  • Custom Tool: GlobalResourceProxyGenerator
  • Custom Tool Namespace: "empty"

Copy files but the application throws the same previous exception which I shared above and resources not work in localmachine, any advice?

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
18666
  • 125
  • 1
  • 18
  • You helped me by asking this questions, because I tried around and didn't know the default "Properties" anymore :D and turning the settings back to the default ones helped for me – Florian K Mar 08 '17 at 14:18

3 Answers3

4

Thanks for help

I did:

  1. Create folder Resources
  2. Move all resources from App_GlobalResources to new folder
  3. Change all file properties:

Build Action: Embedded Resource

Copy to output: Copy always

Custom Tool: PublicResXFileCodeGenerator

Custom Tool Namespace: Resources

This setting allowed me to change Access Modifier resx file to Public.

Now the project work in local machine. File resx during publish are coping to serwer. Server application is work.

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
18666
  • 125
  • 1
  • 18
  • *It is possible create template for new resx file?* Yes there are solutions to simplify the task. You can create a new ItemTemplate or manipulate existing ItemTemplate to perform what you need. – Reza Aghaei Oct 08 '16 at 22:54
  • If you need more help on *It is possible create template for new resx file?* add a new question containing above description and ask your question there. Each stackoverflow post(thread) is dedicated to a single question. For more information see [One post with multiple questions or multiple posts?](http://meta.stackexchange.com/questions/39223/one-post-with-multiple-questions-or-multiple-posts) :) – Reza Aghaei Oct 08 '16 at 22:57
  • Also usually you don't need to post a conclusion. But your answer for this post was OK and useful so I upvoted it. – Reza Aghaei Oct 08 '16 at 22:58
  • If you asked a new question, feel free to notify me :) – Reza Aghaei Oct 08 '16 at 23:07
1

Consider these notes about resources:

When you add a resource file to the App_GlobalResources special folder of an ASP.NET project, the GlobalResourceProxyGenerator custom tool will be used for your resource and it will generates an internal class in Resources namespace in App_GlobalResources assembly for managing resource.

  • These kind of resources are internal and their access modifier can not be changed.
  • They can not be used for data annotations attributes like [Display] or validation attributes.

  • They can be used in View or code directly by calling Resources.ResourceFile.ResourceProperty.

An embedded resource with ResXFileCodeGenerator as custom tool, will generate a public resource file in a namespace which is default namespace + folder hierarchy.

  • These kind of resources are public by default but you can change the access modifier of them using designer. Also you can generate them in a custom namespace by changing their Custom Tool Namespace property.

  • They can be used for data annotations attributes like [Display] or validation attributes.

  • They can be used in View or code directly by calling SomeNamespace.ResourceFile.ResourceProperty.

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
-1

Open the resource file, change the access modifier to public and don't make it as embedded resources as this will generate another assembly with the name of the project assembly and .resources suffix appended, this will make it hard for you to change any resource value after deployment, make it not embedded and copied to output directory in order to have the option to change the resx file later without the need to deploy the dll.

Haitham Shaddad
  • 4,336
  • 2
  • 14
  • 19