ASP.Net Content Delivery Network

date_range  20 January 2013

ASP.Net
Tutorial

I’ve updated this article here.

Whilst working on a project recently I came across a little problem whereby I needed to share LESS and jQuery scripts between MVC applications in the same solution. We all love the url helper methods that are included in MVC to get the path’s of content files using @Url.Content() method that will map a server path to a relative address.

Add Fakes Assembly Solution Layout

Now that I was intending to use a CDN to host the stylesheets and scripts for the applications, that wasn’t going to fly as the files we no longer part of the same application domain path. It also sprung to mind that when locally debugging the application, making small adjustments to the files, I didn’t want to have to push the files up to the CDN everytime, so I decided to make the CDN it’s own project in the solution. The layout of the solution is now as shown in the image. Each of the main projects have a post-build event to start the content project when they are run.

I also made my own Url helper extension so that I didn’t have to manually set the base path of the CDN when publishing. It looks like this:

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

Using a compiler notation I check which configuration the DLL has been built with and then return the absolute path of the resource. It’s not perfect; I’d like it to set the port dynamically for local debugging using a value in the web.config.

I’d love to hear anyone elses take on the problem and any improvements I could make. I’ve got an interesting post to make over the coming days about persistence ignorant use of Entity Framework that I’m hoping should be quite useful.

comments powered by Disqus

chevron_left Archive