Lets see how to store key value pairs in a resource file and how to use them in a Razor view.(Assume our project name is "MyApplication" for examples)
- Create a directory in your project for resource files (Ex: directory named "Localization")
- To create a resource file in that directory,
right click on that directory => click Add => click New Item => Select general tab under Visual C# => Select Resource File => click Add (Give a name to that file as you want ex:MyResource) - Then your resource file will be displayed in a table format in Visual Studio Insert your Name , Value pairs.
- Set "Custom Tool Namespace" value as path to your resource file (ex:MyApplication.Localization)
Now you have created a resource file successfully. Then you just have to use them in Razor views.
for that,
Add this code at the top of your views.
@using Resources = MyApplication.Localization
Here MyApplication is project name and Localization is directory of resource file.
Then when ever you want to use a value in Razor view use this,
@Resources.MyResource.Name
Here "MyResource" is name of your resource file and "Name" name of your key value pair.
No comments:
Post a Comment