ASP.NET MVC - Using Resource Files To Manage String Constants

Introduction

In this article, we will see how we can save strings in the resource file instead of constants spread all over the application. We will put all strings used in the application centrally in the resource file. Now, in this article, we will see how to use resource file in POCO class and display it  in the UI.

  • Open Visual Studio that is installed in your system.
  • Go to File -> New -> Project

    ASP.NET

  • Then, choose Web -> ASP.NET Web Application template and give a name to your solution.

    ASP.NET
  • Choose "Empty" MVC template, as shown below.

    ASP.NET
  • You will see your solution structure like the following.

    ASP.NET
  • Now, add class or POCO Class in Models folder.
  • Right click on Models folder and add class as shown below.

    ASP.NET
  • Give class name as example.cs.

    ASP.NET
  • Now, write the properties/Fields that you want to show in the UI.
    1. namespace ResourceInMVC.Models {  
    2.     public class Employee {  
    3.         public string Name {  
    4.             get;  
    5.             set;  
    6.         }  
    7.         public string Address {  
    8.             get;  
    9.             set;  
    10.         }  
    11.         public string ContactNo {  
    12.             get;  
    13.             set;  
    14.         }  
    15.     }  
  • Now, create a resource file as shown below.
  • Right click on your solution, go to Add >> New Item.

    ASP.NET
  • Type resource in search box. You will get the resource file listed over there. Choose that file and give name with resx extension, as shown in below image.

    ASP.NET
  • Now, add a name value and comments that you want to show in the UI.
  • Remember, always set Access Modifier to public, as highlighted below.

    ASP.NET
  • Now, create an empty Controller. 
  • Right click on Controllers folder, and go to Add >> Controller.

    ASP.NET
  • Choose an empty Controller.

    ASP.NET

    ASP.NET
  • Click on "Add" button, as shown above.
  • Now, right click on Index method and click on "Add View".

    ASP.NET
  • Give model class name as your POCO class name.

    ASP.NET
  • Click on "Add" button.
  • Now, add the code for resource file in your POCO Class.
    1. using System.ComponentModel.DataAnnotations;  
    2. namespace ResourceInMVC.Models {  
    3.     public class Employee {  
    4.         [Display(Name = "NameofthePerson", ResourceType = typeof(StringResources))]  
    5.         public string Name {  
    6.             get;  
    7.             set;  
    8.         }  
    9.         [Display(Name = "AddressofthePerson", ResourceType = typeof(StringResources))]  
    10.         public string Address {  
    11.             get;  
    12.             set;  
    13.         }  
    14.         [Display(Name = "ContactNoofthePerson", ResourceType = typeof(StringResources))]  
    15.         public string ContactNo {  
    16.             get;  
    17.             set;  
    18.         }  
    19.     }  
    20. }  
  • Now, run the solution.

    ASP.NET

Summary

In this article, we have learned how to manage string constants in ASP.NET MVC using Resource files. I hope you liked it.

Up Next
    Ebook Download
    View all
    Learn
    View all