ASP.NET 2.0 Resources

Powered by Blogger

ASP.NET 2.0: Custom Resource Provider Using Sql Database

The world of Web applications is quite different from Windows forms. The solutions that perform great for Windows applications are sometimes quite pain when developing solution designed for the web. ASP.NET 2.0 introduces new feature called declarative localization expressions. It allows you to easily localize web pages and by default it uses .resx files stored in App_LocalResources and App_GlobalResources folders in your web application. Visual studio 2005 supports easy resource creation for each web page by "Generate local resources..." menu entry. Localization to different language/culture is so simple as copying and renaming this automatically generated file and adding desired locale code to it's file name.

But there are also some drawbacks when using this in ASP.NET 2.0 web applications...

  • The resource files (satelite assemblies) are locked all the time, the application is running (it's not easy to update there localized resources).
  • It's difficult (if not impossible) to manage these resources directly from your own web application (for example let the user create new localization or edit existing ones on ther own).

But wait, we are in ASP.NET 2.0. Many thing can be extended and so this Resource provider can be. All you need is simply create your own ResourceProviderFactory based on ResourceProviderFactory class and modify web.config file. Now imagine, you could have your resources stored in Sql (or any other) database.

public sealed class SqlResourceProviderFactory : ResourceProviderFactory
    public override IResourceProvider 
        CreateGlobalResourceProvider(string classKey) {
        return new SqlResourceProvider(null, classKey);
    public override IResourceProvider
        CreateLocalResourceProvider(string virtualPath) {
        return new SqlResourceProvider(virtualPath, null);

And coresponding declaration in web.config file...


This is all you need (except of course writing your own localization resource provider) to swap ASP.NET 2.0 core resource management functionality and suply your own implementation. The ResourceProviderFactory contains two methods...

  • CreateGlobalResourceProvider - used for creating provider for global resources (accessed by GetGlobalResourceObject method or those declaratively used as <%$ Resources:Class, ResourceID %>)
  • CreateLocalResourceProvider - used for local resources (accessed by GetLocalResourceObject method or those declaratively used as meta:resourcekey="Button1")

The IResourceProvider interface that is return by these two methods contains definition for following method and property.

object GetObject(string resourceKey, CultureInfo culture)
IResourceReader ResourceReader

The GetObject method is used to get resource by resourceKey string and specific culture, where ResourceReader property returns all resources for specified virtualPath and current culture. I will not discuss any other details, instead i'm providing links to some custom database ResourceProviders implemented by other users...


Post a Comment

<< Home

Created dolly