Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Register a CDN URL generator for Cassette. See blog post: http://kamranicus.com/blog/2015/10/10/azure-cdn-cassette/
namespace Website {
/// <summary>
/// Configures Cassette IoC
/// </summary>
public class CassetteContainerConfiguration : IConfiguration<TinyIoCContainer>
{
public void Configure(TinyIoCContainer container)
{
// set CDN support
container.Register<IUrlGenerator>(
(c, n) =>
new CdnUrlGenerator(c.Resolve<IUrlModifier>(), c.Resolve<CassetteSettings>().SourceDirectory));
}
}
public class CdnUrlGenerator : IUrlGenerator
{
private readonly IUrlGenerator _urlGenerator;
public CdnUrlGenerator(IUrlModifier urlModifier, IDirectory sourceDirectory)
{
_urlGenerator = new UrlGenerator(urlModifier, sourceDirectory, "cassette.axd/");
}
public string CreateBundleUrl(Bundle bundle)
{
return GetCdnUrl(_urlGenerator.CreateBundleUrl(bundle));
}
public string CreateAssetUrl(IAsset asset)
{
return GetCdnUrl(_urlGenerator.CreateAssetUrl(asset));
}
public string CreateRawFileUrl(string filename, string hash)
{
return _urlGenerator.CreateRawFileUrl(filename, hash);
}
public string CreateRawFileUrl(string filename)
{
return _urlGenerator.CreateRawFileUrl(filename);
}
public string CreateAbsolutePathUrl(string applicationRelativePath)
{
return _urlGenerator.CreateAbsolutePathUrl(applicationRelativePath);
}
public string CreateCachedFileUrl(string filename)
{
return _urlGenerator.CreateCachedFileUrl(filename);
}
private string GetCdnUrl(string relativeUrl)
{
string url = AppSettings.CdnUrl.TrimEnd('/') + relativeUrl;
var ctx = HttpContext.Current;
if (ctx != null)
{
var ctxBase = new HttpContextWrapper(ctx);
// for SSL context, request HTTPS URL otherwise HTTP
// because CDN Origin needs to match
if (ctxBase.IsSSL())
{
url = url.Replace("http://", "https://");
}
else
{
url = url.Replace("https://", "http://");
}
}
return url;
}
/// <summary>
/// Prepends the CDN URL if given
/// </summary>
public class CdnUrlModifier : IUrlModifier
{
public string Modify(string url)
{
url = url.ToLowerInvariant();
if (url.StartsWith("http"))
{
var ctx = HttpContext.Current;
if (ctx != null)
{
var ctxBase = new HttpContextWrapper(ctx);
// for SSL context, request HTTPS URL otherwise HTTP
// because CDN Origin needs to match
if (ctxBase.IsSSL())
{
url = url.Replace("http://", "https://");
}
else
{
url = url.Replace("https://", "http://");
}
}
return url;
}
else
{
return "/" + url;
}
}
}
}
}
<configuration>
<appSettings>
<!-- Local Setting -->
<add name="CdnUrl" value="cassette.axd/" />
<!-- Azure/Production -->
<add name="CdnUrl" value="https://az9999999.vo.msecnd.net/" />
</appSettings>
</configuration>
@chenbrian

This comment has been minimized.

Copy link

commented Oct 23, 2015

How do you handle the hash in the url? For example:
https://az9999999.vo.msecnd.net/script/d81ab7428c71bfd2d3433ebd29a944bd3eef6d9e/js/somejs

How do you handle the bundled js?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.