Skip to content

Instantly share code, notes, and snippets.

@davidwhitney
Created March 20, 2013 01:00
Show Gist options
  • Save davidwhitney/5201521 to your computer and use it in GitHub Desktop.
Save davidwhitney/5201521 to your computer and use it in GitHub Desktop.
Html Helper to support CDN Url.Content locations in ASP.NET MVC
public class Settings : ConfigurationSection
{
[ConfigurationProperty("enabled", DefaultValue = "false", IsRequired = true)]
public bool Enabled
{
get { return (bool)this["enabled"]; }
set { this["enabled"] = value; }
}
[ConfigurationProperty("cloudFrontDomainName", DefaultValue = "", IsRequired = true)]
public string CloudFrontDomainName
{
get { return (string)this["cloudFrontDomainName"]; }
set { this["cloudFrontDomainName"] = value; }
}
public static string SectionName { get { return "frontin"; } }
public static Settings Load()
{
return (Settings)ConfigurationManager.GetSection(SectionName);
}
}
public static class UrlExtensions
{
public static Lazy<Settings> Configuration { get; set; }
static UrlExtensions()
{
Configuration = new Lazy<Settings>(Settings.Load);
}
public static string CdnContent(this UrlHelper helper, string path)
{
var underlying = helper.Content(path);
if (!Configuration.Value.Enabled)
{
return underlying;
}
var cloudRoot = HttpContext.Current.Request.Url.Scheme + "://" + Configuration.Value.CloudFrontDomainName;
return underlying.StartsWith("/")
? cloudRoot + underlying
: underlying.Replace(HttpContext.Current.Request.Url.Host, Configuration.Value.CloudFrontDomainName);
}
}
<configSections>
<section name="frontin" type="Namespace.Settings, Assembly" requirePermission="false" />
</configSections>
<frontin enabled="true" cloudFrontDomainName="xxxxx.cloudfront.net" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment