Last active
March 5, 2016 16:47
-
-
Save kamranayub/2da4ccfec3e7812c8367 to your computer and use it in GitHub Desktop.
Register a CDN URL generator for Cassette. See blog post: http://kamranicus.com/blog/2015/10/10/azure-cdn-cassette/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<configuration> | |
<appSettings> | |
<!-- Local Setting --> | |
<add name="CdnUrl" value="cassette.axd/" /> | |
<!-- Azure/Production --> | |
<add name="CdnUrl" value="https://az9999999.vo.msecnd.net/" /> | |
</appSettings> | |
</configuration> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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?