Reputation: 173
Can anyone tell how to access resource file in class library asp.net mvc4? I'm building a generic Class Library that I will use it in all my projects. I tried with following code:
HttpContext.GetGlobalResourceObject();
Can anyone tell any other method is there to access the resource file in class library?
Upvotes: 5
Views: 2384
Reputation: 194
I would suggest you to create a new library for Resources Once you are done making resources key value pairs then add the reference of your Resource Library in to your project. and then you are able to access the resource library in this way
Namespace.ReourceLibraryName.Key
Upvotes: 2
Reputation: 817
You can add your resource file any where in your class library, but I think your question is how to use it, If your resource files name is Res1.resx and there is a key in it called message then you can call
Res1.message
from your classes in that library. But you I am sure you would like to use it event from out side the library for that you need to make your resource file public, please refer Visual Studio - Resx File default 'internal' to 'public' please do let me know if it is the one you need or some thing else?
Upvotes: 1
Reputation: 2181
When you create your resource you have to set the access level from internal (by default) to public. After this you simply can access your resources (if you reference the assembly) by the name of the resource and the static properties generated into them.
Upvotes: 3
Reputation: 12705
just include System.Web.dll in your class library project and access the resource file just like you would have accessed in the MVC project
HttpContext.GetGlobalResourceObject();
when you add a refrence 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 instance of HttpContext
.
Note:- directly using HttpContext like this would create problems when you will try to test this application
Upvotes: 1
Reputation: 11388
Edit based on the comments, including best practices :
To create a Resource file in MVC4 : In the solution explorer, right click on your project. Then, you click on "Add", then on "Add ASP.Net folder", and then click on App_GlobalResources. On the new folder, you right click. Then you add a new Resource File.
You open then this Resource file and can add new resources. The lefter column is where you set the keys, and the righter one is for the values you have to insert inside it.
Then; it is really easy, you just have to write the following parts of code to access the values you want.
On the c# side, it is :
Resources.NameOfYourResFile.NameOfYourResKey
On the ASP side, assuming that you're using razor, it is :
@Resources.NameOfYourResFile.NameOfYourResKey
Example : If you have created a a file named "Global.resx", and created the key "ApplicationTitle", which has "My super website" as value, you just have to write the following line of code to put it for example into a string :
string siteTitle = Resources.Global.ApplicationTitle;
and that's it
Upvotes: 6