Share via


How to: Migrate an ASP.NET 1.x Web Page to Use Strongly Typed Resources

This topic demonstrates how to migrate an ASP.NET version 1.x Web page that uses resources to an ASP.NET version 2.0 Web page that uses strongly typed resources.

In ASP.NET 2.0 you can create strongly typed resources by putting the resources in the App_GlobalResources folder of you application. A strongly typed resource can be programmatically accessed through the System.Resources namespace. A strongly typed resource can also be accessed declaratively by using a resource expression. Both methods for adding resources to the Web page are shown in the procedure below.

The procedures below require you to use the .NET Framework version 2.0.

To use a resource from an assembly on a Web page

  1. Create a resource file named Resource1.resx that has one string resource named String1 with a value of String from resource.

    For information on how to create a resource file, see ASP.NET Web Page Resources Overview.

  2. Convert the Resource1.resx file into a binary resource file (.resource) using the Resource File Generator (Resgen.exe) with the following command.

    resgen Resource1.resx Resource1.resources
    
  3. Compile the Resource.resource file into an assembly named Resource1Assembly.dll using the following command.

    vbc /t:library /out:bin\Resource1Assembly.dll /res:Resource1.resources
    
    csc /t:library /out:bin\Resource1Assembly.dll /res:Resource1.resources
    
  4. Create a Web page that uses the resource, as shown in the following code example.

    <%@ Page Language="VB" %>
    <%@ Import Namespace="System.Reflection" %>
    <%@ Import Namespace="System.Resources" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <script runat="server">
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
    
            Dim rm As New ResourceManager("Resource1", _
                Assembly.Load("Resource1Assembly"))
            Label1.Text = rm.GetString("String1")
    
        End Sub
    </script>
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>Resource Example</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
          <asp:Label id="Label1"
                     runat="server"/>    
        </div>
        </form>
    </body>
    </html>
    
    <%@ Page Language="C#" %>
    <%@ Import Namespace="System.Reflection" %>
    <%@ Import Namespace="System.Resources" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <script runat="server">
    
        protected void Page_Load(object sender, EventArgs e)
        {
            ResourceManager rm = new ResourceManager("Resource1",
                Assembly.Load("Resource1Assembly"));
            Label1.Text = rm.GetString("String1");
    
        }
    </script>
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>Resource Example</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
          <asp:Label id="Label1"
                     runat="server"/>
        </div>
        </form>
    </body>
    </html>
    
  5. Request the Web page in a browser to verify that it works in the .NET Framework 2.0.

    The Label control text is set programmatically using the resource string. In this example, the ResourceManager class reads the resource from an assembly.

To use a resource in the App_GlobalResoures folder

  1. Using the Resource1.resx file from the previous procedure, add two more string resources: String2 with a value of Second string from resource and String3 with a value of Third string from resource.

  2. Create a folder in the root of your Web application titled App_GlobalResources, if one does not already exist. Put the Resource1.resx file in the App_GlobalResources folder.

    Note

    It is not necessary to create a binary resource file as in the previous procedure, and it is not necessary to manually compile the resource into an assembly.

  3. Create a Web page that uses the resource, as shown in the following code example.

    <%@ Page Language="VB" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <script runat="server">
    
        Protected Sub Page_Load(ByVal sender As Object, _
            ByVal e As System.EventArgs)
    
            Label1.Text = Resources.Resource1.String1
            Label2.Text = CType(GetGlobalResourceObject("Resource1", _
                "String2"), String)
    
        End Sub
    </script>
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>Resource Example</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
           <asp:Label id="Label1" 
                      runat="server"/>
           <br />
           <asp:Label id="Label2"
                      runat="server"/>
           <br />
           <asp:Label id="Label3"
                      Text="<%$ Resources: Resource1, String3 %>"
                      runat="server" />    
        </div>
        </form>
    </body>
    </html>
    
    <%@ Page Language="C#" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <script runat="server">
    
        protected void Page_Load(object sender, 
            EventArgs e)
        {
            Label1.Text = Resources.Resource1.String1;
            Label2.Text = (String)GetGlobalResourceObject("Resource1", 
                "String2");
        }
    </script>
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>Resource Example</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
           <asp:Label id="Label1" 
                      runat="server"/>
           <br />
           <asp:Label id="Label2"
                      runat="server"/>
           <br />
           <asp:Label id="Label3"
                      Text="<%$ Resources: Resource1, String3 %>"
                      runat="server" />
        </div>
        </form>
    </body>
    </html>
    
  4. Request the Web page in a browser to verify that it works in the .NET Framework 2.0.

    The three Label controls on the Web page are set to the three resource strings in the Resource1.resx file. The Label1 control is set programmatically by accessing the compiled resource in the System.Resources namespace. Any resources in the App_GlobalResources folder are automatically compiled into this namespace.

    The Label2 control is set programmatically using the GetGlobalResourceObject method of the TemplateControl class.

    The Label3 control is set declaratively using explicit resource expression syntax. For more information on the syntax of the declarative resource expressions, see ASP.NET Web Page Resources Overview.

See Also

Tasks

How to: Retrieve Resource Values Programmatically

Concepts

ASP.NET Web Page Resources Overview

Reference

System.Resources