Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access Resource File

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?

like image 533
Marsh Avatar asked Sep 05 '13 14:09

Marsh


People also ask

What is a resource file?

A resource file is a text file with the extension . rc. The file can use single-byte, double-byte, or Unicode characters. The syntax and semantics for the RC preprocessor are similar to those of the Microsoft C/C++ compiler. However, RC supports a subset of the preprocessor directives, defines, and pragmas in a script.

How do I add a resource file to the application?

Add new folder named Resources to the application. Add a folder named Pages to the Resources folder. Add a Resource file to the Pages folder and name it IndexModel.en.resx.

What is a resource file?

Resources found in other file types such as .exe, .dll, and .res files are referred to as resources. You can work with resource files and resources from within your project. You can also work with ones that aren't part of the current project or were created outside the development environment of Visual Studio. For example, you can:

How to use resource files in your WPF project?

How to use resource files in your C# WPF project Step 1: Create a new Visual Studio WPF project. Create a new WPF application for this example: Step 2: Add a new class library project Right-click on your solution folder and select Add > New Project. Create a new... Step 3: Create a folder to store ...

How to read a file from Resources folder in Java?

Java – Read a file from resources folder 1. Files in resources folder 1.1 Review the files in the src/main/resources, later we will access the files and print... 2. Get a file from the resources folder. 2.1 The below example demonstrates the use of getResourceAsStream and... 3. Get a file from the ...


2 Answers

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

like image 111
Deblaton Jean-Philippe Avatar answered Sep 21 '22 23:09

Deblaton Jean-Philippe


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.

like image 36
Péter Avatar answered Sep 20 '22 23:09

Péter