This project is read-only.

CDN Support

High traffic websites will often use a CDN (Content Delivery Network) to cache many of the sites resources (such as scripts and stylesheets). If you use a CDN for your website, Assman contains a hook for you to automatically have your script and stylesheet includes pull from your CDN in environments that support it. Here is an example of how to use it:

If we assume your scripts and stylesheets are uploaded to CDN that is accessible via the host "http://www.mycdn.com" and the scripts are in a mycompanyscripts directory and stylesheets are in a mycompanystylesheets directory, then you could create your own class that inherits from the Assman.Registration.ResourceIncludeResolver that would look like this:
using Assman.Registration;

public class MyCdnEnabledIncludeResolver : ResourceIncludeResolver
{
    public override ResolveScriptUrl(string virtualPath)
    {
        if(IsCdnEnabled())
        {
            return "http://www.mycdn.com/mycompanyscripts" + AppRelativeToAbsolute(virtualPath);
        }
        else
        {
            return AppRelativeToAbsolute(virtualPath);
        }
    }

    public override ResolveStylesheetUrl(string virtualPath)
    {
        if(IsCdnEnabled())
        {
            return "http://www.mycdn.com/mycompanystylesheets" + AppRelativeToAbsolute(virtualPath);
        }
        else
        {
            return AppRelativeToAbsolute(virtualPath);
        }
    }

    private bool IsCdnEnabled()
    {
        var cdnEnabledStr = ConfigurationManager.AppSettings["CdnEnabled"] as string;
        bool cdnEnabled;
        if(cdnEnabledStr != null && bool.TryParse(cdnEnabledStr, out cdnEnabled))
            return cdnEnabled;
        else
            return false;
    }
}

To hook up your custom ResourceIncludeResolver class, you would need to add this line somewhere in your ApplicationStartup code:
ResourceIncludeResolver.Instance = new MyCdnEnabledIncludeResolver();

Then, when an include for /Content/Site.css is rendered on your page, it will look like this:
<link rel="stylesheet" type="text/css" href="http://www.mycdn.com/mycompanystylesheets/Content/Site.css" />

Last edited Jun 30, 2011 at 1:06 AM by andyalm, version 2

Comments

No comments yet.