This documentation is archived and is not being maintained.

ResourceManager and ASP.NET

.NET Framework 1.1

The ResourceManager can also be accessed from Web applications using ASP.NET. The code for this small application can be found in the WorldDocs subdirectory, and the important routines are contained in two files: Global.asax and Default.aspx.

The first user request to the WorldDocs application loads Global.asax and executes the Application_Onstart subroutine. Again, this subroutine uses the static CreateFileBasedResourceManager method to create a resource manager to load the text-based resources from files rather than from assemblies. (The previous sample programs also demonstrated this). This ResourceManager is then assigned to the RM variable of the Application. Each user request also executes the Application_BeginRequest subroutine, which results in the user's culture (as specified by the Request.UserLanguages property) to be assigned to the current executing thread on the Web server, as shown in the following code.

Listing 5. Named Resources with ResourceManager (Global.asax)

void Application_OnStart() {
    Application["RM"] = 
       ("mytext", Server.MapPath("resources") 
       + Path.DirectorySeparatorChar, null);

void Application_BeginRequest(Object sender, EventArgs args) {
    // For each request initialize the culture values with
    // the user language as specified by the browser.

    try {
        Thread.CurrentThread.CurrentCulture = new 
    catch(Exception) {
        // provide fallback for not supported languages.
        Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");

    Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;

The dynamic resource assignment is handled as the page is loaded, in this case in the Page_Init and Page_Load subroutines in Default.aspx. Page_Init creates a page-scoped ResourceManager, and assigns the RM variable of the Application to it. The Page_Load subroutine dynamically changes the culture, by automatically posting back to the server whenever the user changes the language selection, as shown in the following code.

Listing 6. Named Resources with ResourceManager (Default.aspx)

void Page_Init(Object sender, EventArgs args) {
    // Get the ResourceManager from the Application object.
    rm = (ResourceManager) Application["RM"];

void Page_Load(Object sender, EventArgs args) {
    String SelectedCulture = MyUICulture.SelectedItem.Text;
    if(! SelectedCulture.StartsWith("Choose")) {
        // If another culture was selected, use that instead.
        Thread.CurrentThread.CurrentCulture = new  
        Thread.CurrentThread.CurrentUICulture = new  

The final piece of code to examine is the one that actually returns the image and string resources.

    <img src="Flags/<%=CultureInfo.CurrentCulture%>.jpg">

The image resource is dynamically determined by building a file specification based on the user's culture code, and the string resource uses the ResourceManager to load a string resource from the appropriate .resources file.