CodeKicks.com
Focus on Microsoft Technologies - Tutorials, Articles, Code Samples.

Thursday, September 14, 2006

Whats with System.Web.UI.WebResourceAttribute in .NET

For those who haven't seen this attribute, its an assembly-level metadata attribute that allows server control developers to mark embedded resources as URL-accessible, and ties in with the "Web Resources" feature introduced in ASP.NET "Whidbey". Briefly said, Web resources allow control developers to package up client files such as scripts, images, style sheets etc. and use them in the generated rendering simply by embedding them into the assembly, rather than having to scatter them into the file system (such as in the aspnet_client folder). More manageable, cleaner, and simpler deployment!
Using Web Resources
Imagine I am writing an HtmlEditor control and I want to use a stock bold button icon. Here's what I'd do:

  1. Embed "Bold.gif" as a resource with the same name into my control's assembly.
  2. Mark it as Web-accessible via the attribute:
        [assembly: WebResource("Bold.gif", ContentType.ImageGif)]
  3. Use the GetWebResourceUrl() method on Page to get a URL that can be rendered out to the client.
        boldButton.ImageUrl = Page.GetWebResourceUrl(typeof(HtmlEditor), "Bold.gif");

And finally, a cool trick...
Imagine that I writing a WebPart control that ships …

Whats with System.Web.UI.WebResourceAttribute?

Post a Comment