ASP.NET WebResource behavior in Web User Control (.ascx file)

Today very talented colleauge of mine sent out to us very nice catch about requesting resources from .ascx file.

So this is the mail (yes, he said OK to publish it :))

Hi All,

pay attention when requesting resources using embedded resource and WebResource.axd.

Generally you use this:

Page.ClientScript.GetWebResourceUrl(GetType(), "MyNamespace.Folder.resourceName.ext")

But in case you do this in ASCX code behind it won’t work (at least for me :))

To make it working you must pass explicit type to the method:

Page.ClientScript.GetWebResourceUrl(typeof(MyType), "MyNamespace.Folder.resourceName.ext")

Or course do not forget to register handler in .config:

<add path="WebResource.axd" verb="GET" type="System.Web.Handlers.AssemblyResourceLoader" validate="true" />

and resources in .cs file:

[assembly: System.Web.UI.WebResource("MyNamespace.Folder.resourceName.ext ", "image/png")]

I just wanted to share this with you. Btw you can reach author of this here :

Thx for sharing Matej.

One comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.