Skip to content

Instantly share code, notes, and snippets.

@ntotten
Created April 17, 2011 22:20
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ntotten/924530 to your computer and use it in GitHub Desktop.
Save ntotten/924530 to your computer and use it in GitHub Desktop.
MVC helper for automatically using ASP.NET AJAX CDN and Windows Azure CDN for static content.
using System;
using System.Web.Mvc;
using System.Collections.Generic;
using System.Configuration;
public static class StaticContentHelpers
{
private const string azureCdnUrl = ".cloudapp.net/cdn";
private const string aspCdnUrl = "ajax.aspnetcdn.com/ajax";
private static IDictionary<string, string> cdnFiles = new Dictionary<string, string>
{
// JQuery
{ "jquery-1.5.2.js", "/jQuery/jquery-1.5.2.js" },
{ "jquery-1.5.2.min.js", "/jQuery/jquery-1.5.2.min.js" },
{ "jquery-1.5.1.js", "/jQuery/jquery-1.5.1.js" },
{ "jquery-1.5.1.min.js", "/jQuery/jquery-1.5.1.min.js" },
{ "jquery-1.5.js", "/jQuery/jquery-1.5.js" },
{ "jquery-1.5.min.js", "/jQuery/jquery-1.5.min.js" },
{ "jquery-1.4.4.js", "/jQuery/jquery-1.4.4.js" },
{ "jquery-1.4.4.min.js", "/jQuery/jquery-1.4.4.min.js" },
{ "jquery-1.4.3.js", "/jQuery/jquery-1.4.3.js" },
{ "jquery-1.4.3.min.js", "/jQuery/jquery-1.4.3.min.js" },
{ "jquery-1.4.2.js", "/jQuery/jquery-1.4.2.js" },
{ "jquery-1.4.2.min.js", "/jQuery/jquery-1.4.2.min.js" },
{ "jquery-1.4.1.js", "/jQuery/jquery-1.4.1.js" },
{ "jquery-1.4.1.min.js", "/jQuery/jquery-1.4.1.min.js" },
{ "jquery-1.4.js", "/jQuery/jquery-1.4.js" },
{ "jquery-1.4.min.js", "/jQuery/jquery-1.4.min.js" },
{ "jquery-1.3.2.js", "/jQuery/jquery-1.3.2.js" },
{ "jquery-1.3.2.min.js", "/jQuery/jquery-1.3.2.min.js" },
// JQuery UI
{ "jquery-ui-1.8.11.js", "/jquery.ui/1.8.11/jquery-ui.js" },
{ "jquery-ui-1.8.11.min.js", "/jquery.ui/1.8.11/jquery-ui.min.js" },
{ "jquery-ui-1.8.10.js", "/jquery.ui/1.8.10/jquery-ui.js" },
{ "jquery-ui-1.8.10.min.js", "/jquery.ui/1.8.10/jquery-ui.min.js" },
{ "jquery-ui-1.8.9.js", "/jquery.ui/1.8.9/jquery-ui.js" },
{ "jquery-ui-1.8.9.min.js", "/jquery.ui/1.8.9/jquery-ui.min.js" },
{ "jquery-ui-1.8.8.js", "/jquery.ui/1.8.8/jquery-ui.js" },
{ "jquery-ui-1.8.8.min.js", "/jquery.ui/1.8.8/jquery-ui.min.js" },
{ "jquery-ui-1.8.7.js", "/jquery.ui/1.8.7/jquery-ui.js" },
{ "jquery-ui-1.8.7.min.js", "/jquery.ui/1.8.7/jquery-ui.min.js" },
{ "jquery-ui-1.8.6.js", "/jquery.ui/1.8.6/jquery-ui.js" },
{ "jquery-ui-1.8.6.min.js", "/jquery.ui/1.8.6/jquery-ui.min.js" },
{ "jquery-ui-1.8.5.js", "/jquery.ui/1.8.5/jquery-ui.js" },
{ "jquery-ui-1.8.5.min.js", "/jquery.ui/1.8.5/jquery-ui.min.js" },
// JQuery Validate
{ "jquery.validate.js", "/jquery.validate/1.8/jquery.validate.js" },
{ "jquery.validate.min.js", "/jquery.validate/1.8/jquery.validate.min.js" },
// ASP.NET MVC3
{ "jquery.unobtrusive-ajax.js", "/mvc/3.0/jquery.unobtrusive-ajax.js" },
{ "jquery.unobtrusive-ajax.min.js", "/mvc/3.0/jquery.unobtrusive-ajax.min.js" },
{ "jquery.validate.unobtrusive.js", "/mvc/3.0/jquery.validate.unobtrusive.js" },
{ "jquery.validate.unobtrusive.min.js", "/mvc/3.0/jquery.validate.unobtrusive.min.js" },
};
public static string StaticContent(this UrlHelper urlHelper, string contentPath)
{
if (String.IsNullOrEmpty(contentPath))
throw new ArgumentNullException("contentPath");
string[] parts = contentPath.Split('/');
string file = parts[parts.Length - 1].ToLowerInvariant();
if (cdnFiles.ContainsKey(file))
{
return String.Concat(urlHelper.RequestContext.HttpContext.Request.Url.Scheme, "://", aspCdnUrl, cdnFiles[file]);
}
string relativePath = urlHelper.Content(contentPath);
string cdnNamespace = ConfigurationManager.AppSettings["WindowsAzureCdnNamespace"];
if (cdnNamespace == "set_this_value")
{
throw new ConfigurationErrorsException("The value for WindowsAzureCdnNamespace in the web.config appSettings is invalid. Either set this value to your Azure CDN namespace or remove the setting.");
}
if (cdnNamespace != null)
{
return String.Concat(urlHelper.RequestContext.HttpContext.Request.Url.Scheme, "://", cdnNamespace, azureCdnUrl, relativePath);
}
return relativePath;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment