Implement CDN with ASP.NET website

There’s no reason not to use a CDN service like such as Akamai, Amazon CloudFront or Windows Azure, especially when most of end-user response time is spent downloading static resources like images, css and scripts.

Regardless of your CDN choice, once you have your assets in place you should mirror the same folder structure on your CDN. This will allow you to switch between locally deployed assets and those on the CDN.

On code level you can use something like:

<img src='<%= GetCDNPath()assets/images/sample.png' />

Here method “GetCDNPath()” picks up the CDN url and pre-appends it to the relative path of static resource.

public string GetCDNPath() {
      get { return ConfigurationManager.AppSettings["CDNPath"].ToString(); }
}

In web.config file, you need to add CDNPath key-value under appSettings element:

<appSettings>
      <add key="CDNPath" value="http://my.cdn.com/user/"/>
</appSettings>

Enjoy!!

(Visited 265 times, 1 visits today)