ASP.NET Resource Provider Model

For web based applications ASP.NET provides a Resource Provider Model but again this only works for assembly embedded resources. The Resource Application Block includes a Web Resource Provider that implements the ASP.NET Resource Provider Model to enable any Resource Application Block resource provider to be used.

The Web Resource Provider is NOT an EntLibContrib.Resource.IResourceProvider like the XmlResourceProvider, say, but instead implements System.Web.Compilation.IResourceProvider provided from ASP.NET. To all intents and purposes the function is the same.

The ASP.NET Resource Provider Model has the concept of local resources for each individual web page and global resources for the whole application. All of the local resources are compiled into one assembly and all of the global resources into another. In addition ASP.NET will add a strongly typed class for each of your global resource sets. The other oddity is that the assemblies get given generated names and placed into temporary folders so finding things can be quite a challenge.

Adding and Editing Resources in ASP.NET

Global resources are added and edited manually and placed into the App_GlobalResources folder. These are then edited using the standard resx resource editor built in to Visual Studio.

Local resources on the other hand are generated for each web page by selecting the menu option Tools|Generate Local Resources whilst viewing your web page in design mode. An Xml resource file is created for you in the App_LocalResources folder with a name like webpagename.aspx.resx. Local resouces can be generated for other object types too like user controls, master pages and sitemaps. Of course if you want additional cultures then these must be added manually so our generic french web page would be webpagename.aspx.fr.resx.

All control properties on a web page that are decorated with the LocalizableAttribute have their property values stored in the resources file with a key based on the control and property name. So if the control is a button with an Id of Button1 then its Text property will be stored with a key of Button1Resource1.Text, the Resource1 addition to the control Id name seems to be arbitrarily added.

Getting at Resources in ASP.NET

There are three ways you can get at resources in ASP.NET
  • You can use the strongly typed resource classes generated for each Global resource file in the code-behind for any web page and in any server based classes in the same way as you would for any other type of application.
  • When you generate local resources from a web page or other localizable object type you will be using those resources implicitly and ASP.NET will automatically mark up your ASPX files with meta resource tags similar to the following:

<asp:Button ID="Button1" runat="server" meta:resourcekey="Button1" />


Note here we have our button Button1 again and all localizable properties will be found in the list of resources using the resourcekey. The class name required by the resource provider is implied from the code-behind class of the web page.
  • You can also mark up your web page explicitly to use either local or global resources with syntax like the following:

The following is an example of the explicit use of local resource
<asp:Button ID="Button1" runat="server" Text="<%$ Resources:Button1.Text %>"  />


The following is an example of the explicit use of a global resource where the resource file is MyGlobals and the resource key is ButtonName
<asp:Button ID="Button1" runat="server" Text="<%$ Resources:MyGlobals, ButtonName %>" />

Using the Resource Application Block

The ASP.NET resource provider model can be extended by creating two classes, a factory class and a provider class. The factory is responsible for generating instances of provider classes to handle local resources and global resources. The provider is responsible for getting those resources and has just a read-only ResourceReader property that returns an IResourceReader type and a GetObject() method that returns a resource object.

The WebResourceProviderFactory

The WebResourceProviderFactory is the factory provided within the Resource Application Block to support the ASP.NET resource provider model. To use it you either have the assembly EntLibContrib.Resource.dll installed in the GAC or added to the web project as a reference. Then you configure the globalization section of your web.config as such:

[web.config]
<globalization resourceProviderFactoryType="EntLibContrib.Resource.Web.WebResourceProviderFactory, EntLibContrib.Resource" />

The WebResourceProvider

Instances of the WebResourceProvider class are created by the factory so you don't actually see or manipulate this class in your code as such.

Configuring your Resource Managers

I mentioned in the Configuration section that there was one situation where resource manager names, also known as the instance name, would be predetermined and that is when using the Resource Application Block for local resources. The reason is that the class name is used by the resource application block as the instance name and the class name is determined by the web page or other localizable object's code-behind class name, so you will need to configure a resource manager for every localizable object in your web application. These can, of course, all point to the same resource store however. Note: there is a special case for the Default.aspx web page, in that the class in the code-behind is actually _Default but the ASP.NET provider model passes the text default instead.

As far as getting local resources into your chosen resource stores I recommend that you use the Tools|Generate Local Resources feature to create an Xml resource file and then copy and paste (or transfer) these resources to your own store.

As for global resources you can use whatever instance names you like as this name will be used in explicit declarations or in code-behind classes in the normal fashion of using resources, of course you can always use the Resource Application Block strongly typed resource class generator to create your own resource classes for using global resources in code-behind classes.

Getting at Resources using the Resource Application Block

There are the same three ways you can get at resources in ASP.NET using the Resource Application Block
  • You can use the strongly typed resource classes generated from the Enterprise Library Configuration Console in the code-behind for any web page and in any server based classes in the same way as you would for any other type of application.
  • Implicit usage for local resources will happen in the same way as before except you must make sure that the resource manager is named the same as the web object code behind class. In the following example if the web page was called HomePage.aspx then you would need to configure a resource manager called HomePage and make sure it contained a set of resources with the key names Button1Resource1.propertyname these, of course, can be generated for you using the option from the Tools menu:

<asp:Button ID="Button1" runat="server" meta:resourcekey="Button1" />

  • Explicit use of resources is no different for local resources as the example above but for global resource you have to explicitly include the resource manager instance name:

The following is an example of the explicit use of local resource note that there is no change to what we had before and the same rule applies to the naming of the resource manager instance.
<asp:Button ID="Button1" runat="server" Text="<%$ Resources:Button1.Text %>"  />


The following is an example of the explicit use of a global resource where the resource manager instance name is MyGlobals and a resource in this set has a key of ButtonName
<asp:Button ID="Button1" runat="server" Text="<%$ Resources:MyGlobals, ButtonName %>" />


For a full working sample see the web application in The QuickStart Sample Applications

Last edited Dec 17, 2009 at 12:32 PM by ewdev, version 2

Comments

No comments yet.