Rick Strahl's Weblog  

Wind, waves, code and everything in between...
.NET • C# • Markdown • WPF • All Things Web
Contact   •   Articles   •   Products   •   Support   •   Advertise
Sponsored by:
West Wind WebSurge - Rest Client and Http Load Testing for Windows

Hooking meta:Resource parsing in ASP.NET 2.0?


:P
On this page:

I’m mucking around today with some internationalization code in a control that is meant to take over localization instead of the standard resource providers pulling data from a database. I have most of this functionality working but now I’m trying to figure out some better ways to integrate this into the localization scheme.

 

Hooking up the functionality is not difficult thanks to the provider model that ASP.NET 2.0 has introduced which allows me to replace the existing ResourceManager with my own custom one. This makes it easy to hook up custom processing to any calls to Page.GetLocalResourceObject()/GetGlobalResourceObject() as well as the parsing of <%$ Resources:LinkButton1Resource1.Text %> expressions which gets a me a long way there.

 

However, one very cool feature that ASP.NET provides is the ability to use this construct:

 

meta:resourcekey="TextBox1"

 

which allows ASP.NET to automatically map a control and any resource properties. So if TextBox1 exists and has resources defined for TextBox1.Text and TextBox1.ToolTip both of these properties are automatically localized which results in generated code in the BuildControl_ code of the ControlTree:

 

box1.ToolTip = Convert.ToString(base.GetLocalResourceObject("TextBox1Resource1.ToolTip"),

                                CultureInfo.CurrentCulture);

box1.Text = Convert.ToString(base.GetLocalResourceObject("TextBox1Resource1.Text"),

                             CultureInfo.CurrentCulture);

 

Great, except that this appears to be a compile time feature with ASP.NET going out at compile time parsing the resource file and source code file and mapping these properties together. The meta: tag is stripped. This means this code is generated at compile time and if the ResourceKey doesn’t exist in the resource file nothing is generated.

 

Does anybody know if there’s some way to intercept this parsing mechanism in any way or some sort of interface that can be trapped to allow hooking into this and provide ASP.NET with a list of matches here in the same fashion?

 

I know that custom build providers can be plugged in for things like the <%$ Resources:LinkButton1Resource1.Text %> expressions, but I don’t think this applies to these meta tags.

 

Anybody have any idea where to look for this?


The Voices of Reason


 

Jason Haley
September 09, 2006

# Interesting Finds: September 9, 2006


Dominick
September 09, 2006

# re: Hooking meta:Resource parsing in ASP.NET 2.0?

Rick -

since resource access is also provider based, maybe a custom resource provider would do the trick for you.

Rick Strahl
September 09, 2006

# re: Hooking meta:Resource parsing in ASP.NET 2.0?

Dominick, I'm half way down that path at this point, but I don't see any interfaec that could be called to provide that data to the provider, so I don't think the provider will pick up the values. I'll have to wait until I get done with the provider implementation to be sure (need it anyway <s>) but so far it doesn't look hopeful.

Wilco Bauwer
September 09, 2006

# re: Hooking meta:Resource parsing in ASP.NET 2.0?

There are implicit (meta:resourcekey) and explicit (<%$, etc.) ways to use resources. For both you can create a custom provider. To do so, implement ResourceProviderFactory and hook it up in the web.config's globalization section (using the resourceProviderFactoryType attribute). The type of the instance you return in CreateLocalResourceProvider should implement both IResourceProvider AND IImplicitResourceProvider. ASP.NET at some point checks if you implement this interface, and only if you do it will use your resource provider for implicit resources.

It's been a while since I looked into this in-depth, so please let me know if this doesn't help.

Rick Strahl
September 09, 2006

# re: Hooking meta:Resource parsing in ASP.NET 2.0?

Ah... IImplicitResourceProvider is the piece I'm probably missing here. Thanks Wilco!

Bertkid
September 25, 2006

# re: Hooking meta:Resource parsing in ASP.NET 2.0?

Has anyone figured out how to implement IImplicitResourceProvider?

Michelsen
October 03, 2006

# re: Hooking meta:Resource parsing in ASP.NET 2.0?

I have excactly the same problem. My project runs fine when I'm JIT compiling my project, but as soon as I ask VS or aspnet_compile to make a compilation, I get a stack of "Object not assigned"-like messages.

Furthermore, the call type "Resources.Web.sitemap.Something" doesn't work. This is something that gives a really nice code when your are using .Resx files, but this doesn't work at all with a custom provider.

Have any of you had luck with these two things? The SDK says that implicit resources should work fine with a custom provider, requiring no implementation... apparantly only when you don't intend to actually precompile your pages?

Rick Strahl
October 03, 2006

# re: Hooking meta:Resource parsing in ASP.NET 2.0?

Michelsen, they work. In fact, I'm sitting here tonight building a custom provider and it works although the interfaces for this are a bitch to get right. You'll get that object error if a resource cannot be found for a given resource key - you should actually see that during compile time as well because the compiler locks in that key and resource reference.

There are a lots of issues to watch out for like the fact that local resources are passed in without a culture (null) so you have explicitly assign a culture if your code is doing any sort of culture specific lookups into a ResourceSet.

Michelsen
October 03, 2006

# re: Hooking meta:Resource parsing in ASP.NET 2.0?

Hi Rick,

I would be really thrilled if you could help me then - I invariably get this message every time I try to compile:

error ASPPARSE: Object reference not set to an instance of an object.

I have fallback etc. in a stored procedure in MSSQL, and entries are written to the DB every time a lookup fails, letting the app continue with temp data.

As I said I have zero problems when I'm JIT-viewing the project, only when I'm trying to precompile, every single meta:resourceKey statement crashes the compile with the above message.

If this is getting too specific for this post, please write me at ole@michelsen.dk - I would highly appreciate your help, since I have several large projects I'm moving from .resx, and it really sux to be stuck midstream :-)

AndrewSeven
November 16, 2006

# re: Hooking meta:Resource parsing in ASP.NET 2.0?

I would like to use meta:resourcekey with a resx file that is shared between 4 aspx pages. :S
Am I SOL?

Rick Strahl
November 16, 2006

# re: Hooking meta:Resource parsing in ASP.NET 2.0?

Andrew, unfortunately that doesn't work as meta:resourcekey is a local resource and so will always look at the matching resx file for the page/control/master.

# DotNetSlackers: Hooking meta:Resource parsing in ASP.NET 2.0?


Laksh
December 11, 2007

# re: Hooking meta:Resource parsing in ASP.NET 2.0?

Michelsen, rick
are you guys able to figure out "Object reference not set to an instance of an object."? i'm having the same problem and i'm not able to figure out how can i solve this issue. I'm using CustomResource provider as described in this article http://msdn2.microsoft.com/en-us/library/aa905797.aspx. but in Provider Factory when i create LocalResourceProvider i want to change vitualpath(resourceType field in DB) depending on what i get through quesrystring. but if i do that i get the object reference error. if you guys could help me i'll be really appreciated. my email id is lax4u@hotmail.com

DrkMag
June 13, 2008

# re: Object Reference not set

Hi Rick,

First your Localization sample is awsome ! I implemented my own resource provider based on your sample but I am getting a Object Reference not set when using meta: tags in source code. The strings are in the database ( SQL Server ) and even when running a trace I see no calls made to the server.

Do you know what VS needs when generating meta: resources when compiling the web site

Any help appreciated

Thanks !

Rick Strahl
June 13, 2008

# re: Hooking meta:Resource parsing in ASP.NET 2.0?

Not sure what you mean by meta tags in source code. The meta tags are basically compiler fixups, so this is a parser/compiler feature rather than something you can do in code.

One thing to remember is that if you have missing meta tags there tend to be problems. If you define a meta tag you should match it otherwise you get those object ref errors. If you use the provider I created and implements IImplicitResourceProvider it shouldn't throw those errors - they are then just ignored. The default provider shows errors.

Sometimes a full rebuild of the project can help (especially when not using WAP)...

DrkMag
June 13, 2008

# re: Hooking meta:Resource parsing in ASP.NET 2.0?

Thanks for the quick reply Rick.

When using the following syntax in .aspx files

<cc1:CtButton ID="CtButton1" runat="server" onclick="CtButton1_Click"
Text="Go Back" meta:resourcekey="CtButton1Resource1"></cc1:CtButton>

when compiling the web site I get the ObjectReference not set.

Now in the table I have rows for cultures '', 'fr-Ca' and 'en-CA' for the ResouceId CtButton1Resource1.Text and CtButton1Resource1.ToolTip.

Also I tried to modify the code behing the IImplicitResourceProvider to the following to be sure that the object returned was not null but even when setting breakpoints it appears the compiler is not using that dll instance but another one.

object IImplicitResourceProvider.GetObject(ImplicitResourceKey implicitKey, CultureInfo culture)
        {
            object oValue = this.ResourceManager.GetObject(ConstructFullKey(implicitKey), culture);
            if (oValue == null)
            { return ConstructFullKey(implicitKey) + " could not be loaded"; }
            else
            { return oValue; }
        }

Boby
August 19, 2008

# How to Take Javascript Error Message From Resource File


Hi,

Am very new to ASP.net Technology, currently am working for a ASP.Net 3.5 Application :(,
so am stucking up every where, currently am working for Language Localization.

I have one some javascript validations in my application, i want to take the messages from Resource file, how can i take the resource file value in javascript...

somebody please help me......

Boby

Torsen
May 08, 2009

# re: Hooking meta:Resource parsing in ASP.NET 2.0?

DrkMag:
Ran into the same problem as you had, and after two days tearing my hair off, I've found where it was stuck. The problem is only when you are working with web sites, and not with web application.

The function CreateLocalResourceProvider in DbSimpleResourceProviderFactory calls WebUtils.GetAppRelativePath(virtualPath) to strip off the virtual path of the web application. Problem is that this is getting called during pre-compilation and the WebUtils.GetAppRelativePath is referring to HttpContext.Current.Request. This is null and therefore you get the 'Objectreference not set' error.

Torsen
May 08, 2009

# re: Hooking meta:Resource parsing in ASP.NET 2.0?

What I did was making another GetAppRelativePath with an additional parameter:

public static string GetAppRelativePath(string logicalPath, string basepath)
{
            logicalPath = logicalPath.ToLower();
            string appPath = basepath; //HttpContext.Current.Request.ApplicationPath.ToLower();
            if (appPath != "/")
                appPath += "/";
            else
                // Root web relative path is empty - strip off leading slash
                return logicalPath.TrimStart('/');

            return logicalPath.Replace(appPath, "");
}


And in DbSimpleResourceProviderFactory, where the function is called, I changed it to:

string ResourceSetName = WebUtils.GetAppRelativePath(virtualPath, DbResourceConfiguration.Current.DesignTimeVirtualPath);


And now it works fine for my Web Site project.

Btw, I also did a convertion for MySql syntax in DbResourceDataManager.cs.

West Wind  © Rick Strahl, West Wind Technologies, 2005 - 2024