Juri Strumpflohner
Juri Strumpflohner Juri is a full stack developer and tech lead with a special passion for the web and frontend development. He creates online videos for Egghead.io, writes articles on his blog and for tech magazines, speaks at conferences and holds training workshops. Juri is also a recognized Google Developer Expert in Web Technologies

ASP.net: Organize Your Resources the Correct Way!

3 min read

ASP.net provides well-known mechanisms for localizing web applications, namely by extracting hard-coded strings into resources files. Localization for Pages and UserControls is fully automated and available from within Visual Studio. While most of these issues may seem quite obvious to you, I think it is still worth mentioning some organizational facts related on how to distribute local vs. global resources. I always again encounter devs using the same resource messages repeated over and over again distributed onto many local, page-related resource files. This makes it an extremely annoying process if you happen to encounter the need for changing them.

"App_LocalResources" vs. "App_GlobalResources" folder
Mainly there are two different types of resources, those available locally to the target element that has been localized (i.e. for the ASP.net Page or UserControl) and those that are globally available to the whole application.

Local resources are being generated by opening the design view of a Page or UserControl and by then clicking on the Visual Studio menu "Tools > Generate Local Resource". This automatically generates appropriate resource files in the "App_LocalResources" folder.
Moreover it adds "meta:resourcekey" tags to the localized server controls on the ASPX code:
All of the generated resources within the App_LocalResources file are just locally available to each Page/UserControl they belong to.

On the other side, App_GlobalResources are - as the name already suggests - globally available to the whole application. They are normally not generated automatically, but created on demand. If the App_GlobalResources folder doesn't yet exist, it can be created by right-clicking on the project in the Visual Studio Solution Explorer and by then choosing the appropriate menu entry (as shown in the following figure)

Which resources are good candidates to become global?
Resources that are good candidates to become global and should therefore be placed in the App_GlobalResources folder are all those which may appear to be replicated throughout the application and are not specific to a certain Page or UserControl.

Examples are:
  • Common action names such as "Save", "Delete", "Back",...
  • Common, generic messages such as "Successfully saved", ...
Which resources are good candidates to go into the App_LocalResources folder?
Simply all of the resources that only belong to the Page or UserControl itself and are not shared among different entities.

How to access resources in the ASPX code?
Local Resources
Local resources can be accessed in the ASPX code either by specifying a meta:resourcekey tag (which is also automatically done by generating them through VS) or as follows:
<%= GetLocalResourceObject("resourcekey") %>

Global Resources
Global resources can be accessed by using the following syntax
<%$ Resources:GlobalResourceFilename, MY_KEY_IN_THE_RESOURCE_FILE %>  

How to access resources in the C# code?
Local Resources

Global Resources
GetGlobalResourceObject("GlobalResourceFilename", "resourceKey");  

Questions? Thoughts? Hit me up on Twitter
comments powered by Disqus