Tuesday, July 1, 2014

Using Resource Files in your ASP.NET Web Application

If you have ever read about Resource Files in the past, you may have heard of them in regards to being used for supporting Globalization in your application.

However, what you may not know, is that they can be used as a general method for storing any type of data that you want to keep from "hard coding" in your application!

For example, you probably have written at least one ASP.NET Web Application that provides confirmation messages or some other types of notifications to the end user when they perform operations in your application.  Most likely, you have directly added these strings into the code that you were using or even perhaps stored them as constants or static variables and then referenced them throughout your Web Application.

Well, fortunately, you can use Resource Files as an alternative to doing that!  The major benefit of doing this is that the Resource Files are STRONGLY TYPED, therefore, you can use them directly in your ASP.NET Web Application without doing anything complex to retrieve content out of them. 

Even better, you can store pretty much anything inside of resource files ranging from strings, images, icons, audio or files!!

  1. In order to get started with adding a resource file to your Application, you simply right-click and select Add-->New Item.  
  2. You then search for a Resources File.  Select it and give it a name.
  3. Once you have added the Resources File to your Web Application, you can begin adding content to it.  
  4. By default, you can add Strings resources to your file, but if you want to add other types of content, simply select from the Strings pull down menu and select the type of resource that you want to use instead.
  5. Then, when you want to reference the particular item from the Resource File in your application, you simply use the name of the resource file and the key in the resource file such as MyResourceFile.ConfirmationMessage.  
  6. That is all there is to it!!

No comments:

Post a Comment