Adding embedded resource with ClientDependency

by Shannon Deminick 3. August 2010 05:32

The uComponents project for Umbraco is coming along rather nicely! So far there are 13 new data types created and a few extensions, hopefully soon we’ll have a package ready to go. In the meantime, I thought I’d share a nice little code snippet that we’re using this throughout uComponents that allows you to add embedded resources to ClientDependency. It’s pretty easy and works perfectly with Umbraco 4.5 or any other site you have running ClientDependency. This will ensure that embedded resources (JavaScript or CSS) are added to the ClientDependency combined scripts/styles and also compressed and cached.

First, I’ll show you how to register your embedded resources using our extension method class (for a Control object):

this.AddResourceToClientDependency(
    "DataTypesForUmbraco.Controls.Shared.Resources.PrevalueEditor.css", 
    ClientDependency.Core.ClientDependencyType.Css);

The above code assumes:

  • The class that you are consuming the code inherits from System.Web.UI.Control
  • That your embedded resource’s path is: DataTypesForUmbraco.Controls.Shared.Resources.PrevalueEditor.css
  • That your embedded resource is a CSS type

Pretty easy right!! Well, the only thing missing is that you’ll need to add our extension method class to your project which looks like this:

/// 
/// Extension methods for embedded resources
/// 
public static class ResourceExtensions
{

    /// 
    /// Adds an embedded resource to the ClientDependency output by name
    /// 
    /// 
    /// 
    /// 
    public static void AddResourceToClientDependency(this Control ctl, string resourceName, ClientDependencyType type)
    {
        //get the urls for the embedded resources           
        var resourceUrl = ctl.Page.ClientScript.GetWebResourceUrl(ctl.GetType(), resourceName);

        //add the resources to client dependency
        ClientDependencyLoader.Instance.RegisterDependency(resourceUrl, type);
    }

    /// 
    /// Adds an embedded resource to the ClientDependency output by name and priority
    /// 
    /// 
    /// 
    /// 
    public static void AddResourceToClientDependency(this Control ctl, string resourceName, ClientDependencyType type, int priority)
    {
        //get the urls for the embedded resources           
        var resourceUrl = ctl.Page.ClientScript.GetWebResourceUrl(ctl.GetType(), resourceName);

        //add the resources to client dependency
        ClientDependencyLoader.Instance.RegisterDependency(priority, resourceUrl, type);
    }

}

So basically, if you have a Control, you can register your embedded resources in ClientDependency with one line of code… sweeeeet.

Categories: .Net | ClientDependency

Comments

8/3/2010 1:29:28 PM #

Pingback from topsy.com

Twitter Trackbacks for
        
        FARMCode.org | Adding embedded resource with ClientDependency
        [farmcode.org]
        on Topsy.com

topsy.com

8/30/2010 5:13:39 AM #

VirtualPathUtility Fixed in .Net 4

VirtualPathUtility Fixed in .Net 4

Shazwazza

11/3/2010 11:34:06 AM #

No, no, no - it's so much more complicated than that!

For embedded resources to work, you also need the WebResource attribute stuff above your namespace:
[assembly : WebResource("...", "text/javascript")]

And, also, when you call Page.ClientScript.GetWebResourceUrl, you need to specify a type that is in the same namespace as the resource.
Calling ctl.GetType() might work sometimes, but it's not going to work all the time!

Better to do something like this:

public static void AddResourceToClientDependency(this Control ctl, Type typeInSameNamespaceAsResource, string resourceName, ClientDependencyType type)
{
    var resourceUrl = ctl.Page.ClientScript.GetWebResourceUrl(typeInSameNamespaceAsResource, resourceName);

    ClientDependencyLoader.Instance.RegisterDependency(resourceUrl, type);
}

Andrew Australia

11/3/2010 11:36:59 AM #

Yes, you are correct, though i did mention in the article that it assumes you know this about embedded resources.

ShannonDeminick Australia