Access Resource File

Can anyone tell me how to access the resource file in asp.net mvc4 class library? I am creating a universal class library that I will use in all my projects. I tried with the following code:

HttpContext.GetGlobalResourceObject(); 

Can anyone tell any other method to access the resource file in the class library?

+4
source share
5 answers

Edit based on comments, including best practices:

To create a resource file in MVC4: In the Solution Explorer, right-click on your project. Then you click Add, then Add ASP.Net Folder, and then click App_GlobalResources. In the new folder, right-click. Then you add a new resource file.

You open this resource file and you can add new resources. The lefter column is where you set the keys, and the earlier one is for the values ​​you must insert inside it.

Then; it is very simple, you just need to write the following parts of the code to get the values ​​you need.

On the C # side, this is:

 Resources.NameOfYourResFile.NameOfYourResKey 

On the ASP side, assuming you are using a razor, this is:

 @Resources.NameOfYourResFile.NameOfYourResKey 

Example: If you created an aa file named "Global.resx" and created an "ApplicationTitle" key that has the value "My Super Site" as the value, you just need to write the following line of code to put it, for example, in the line :

  string siteTitle = Resources.Global.ApplicationTitle; 

and what is he

+5
source

When you create your resource, you must set the access level from internal (by default) to public. After that, you can simply access your resources (if you refer to the assembly) by the name of the resource and the static properties generated in them.

+3
source

I would suggest you create a new library for resources. After you finish creating key pairs of resource values, add the link of your resource library to your project. and then you can access the resource library this way.

Namespace.ReourceLibraryName.Key

+2
source

just include System.Web.dll in the class library project and get access to the resource file in the same way as you could access in the MVC project

 HttpContext.GetGlobalResourceObject(); 

when you add a link to this class library project inside your MVC project and run the project, this code will be executed in the context of the MVC project, and you will get the correct HttpContext instance.

Note: - direct use of the HttpContext, as this would cause problems when you try to test this application

+1
source

You can add your resource file anywhere in your class library, but I think your question is how to use it. If your resource file name is Res1.resx and it contains a key called a message, you can call

  Res1.message 

from your classes in this library. But you, I'm sure, you would like to use this event from the library side, since you need to make your resource file public, please consult Visual Studio - Resx File default 'internal' to "public" please let me know if this is the one that you need, or something else?

+1
source

Source: https://habr.com/ru/post/1500749/


All Articles