ASP.Net Content Delivery Network Part Deux

date_range  22 September 2013

.Net Development

My old post on this topic gets quite a bit of traffic, so as I’ve done quite a bit of work on it I thought I’d add an update on it.

I added the code to my application framework and since then it has evolved quite considerably. It’s now part of my JamesDibble.ApplicationFramework.Web.Mvc.ExtendedWebViewPage so it can now used on any .cshtml in the following way:

To use the class add a reference to the assembly and make sure you put this in your Web.config file in the views folder, NOT the root one.

// ReSharper disable CheckNamespace 
namespace System.Web.Mvc 
// ReSharper restore CheckNamespace 
    using System.Globalization; 
    /// <summary> 
    /// Extensions for MVC 
    /// </summary> 
    public static class MvcExtensions 
        /// <summary> 
        /// Get the absolute path for a resource stored in the CDN. 
        /// </summary> /// <param name="helper"> 
        /// The helper. 
        /// </param> 
        /// <param name="resourceLocation"> 
        /// The resource location. 
        /// </param> 
        /// <returns> 
        /// The >see cref="string"/>.         
        /// </returns> 
        public static string Resource(this UrlHelper helper, string resourceLocation) 
            #if DEBUG 
            return string.Format(CultureInfo.CurrentCulture, "http://localhost:6418/{0}", resourceLocation); 
            return string.Format(CultureInfo.CurrentCulture, "{0}", resourceLocation); 

Then you can use you CDN resources like this:

&lt;script type="text/javascript" src="@this.Resource("Javascript", "home.min.js")"&gt;&lt;/script&gt;

The first parameter is the name of a path configured in the Web.Config. You’ll need to reference JamesDibble.ApplicationFramework.Configuration.dll and then add something like this:

&lt;section name="applicationConfiguration" type="JamesDibble.ApplicationFramework.Configuration.ApplicationConfigurationSection, JamesDibble.ApplicationFramework.Configuration" /&gt; 
&lt;applicationConfiguration environment="Dev"&gt; 
&lt;baseTitle title="My Website - "/&gt; 
&lt;ResourceLocations basePath=""&gt; 
&lt;Resource resourceType="Image" path="image/"/&gt; 
&lt;Resource resourceType="Style" path="style/website/"/&gt; 
&lt;Resource resourceType="Javascript" path="script/"/&gt; 

This section here is specifically for a development environment. I use web deploy so lets use xdt and transform our Release configuration to use you Live environment.

&lt;applicationConfiguration environment="Live" xdt:Transform="SetAttributes(environment)"&gt; 
&lt;ResourceLocations basePath="" xdt:Transform="SetAttributes(basePath)"/&gt; 

Awesome huh? Now we don’t have to change a single thing and we can keep our development changes away from the live deployed code, it all gets sorted for us when we deploy.

Something I’ve added very recently is a tag to use minified sources where files are Javscript or CSS. I use Web Essentials for Visual Studio for both of these types of files which automatically places a minified version of the code with the source. I’ve automatted the process even more by adding useMinified=”false” into the ResourceLocations node so when I’m on a development environment the application serves up the non minified, debuggable code. Lovely jubbly.

comments powered by Disqus

chevron_left Archive