Embedded WebResource

The job of a framework library is to provide a basic package of necessary services and components.

In ASP.NET, being a web application, the UI portion of the framework has to provide not only server side code, but also CSS, JavaScript and image files.

ASP.NET 2.0 made it easy with the WebResourceAttribute.  And I was hunting for a bug today:

Page.ClientScript.RegisterClientScriptResource(GetType(),
"My.Framework.Web.UI.Pages.Collapse.js");

We have this bit of code on the masterpage in the framework, as soon as we use this in applications it stops working.


We get a 404, "invalid webresource request".


The issue actually was quite easy to fix once we what the Type argument was for.  It's for ASP.NET to work out which assembly to look for the resource.


Page.ClientScript.RegisterClientScriptResource(typeof(ATypeInTheFrameworkAssembly), "My.Framework.Web.UI.Pages.Collapse.js");


Which fixed the problem.