CDN for Images in ASP.NET

Posted by Chris on Stack Overflow See other posts from Stack Overflow or by Chris
Published on 2010-03-27T19:49:55Z Indexed on 2010/03/27 19:53 UTC
Read the original article Hit count: 212

Filed under:
|
|
|
|

I am in the process of moving all of the images in my web application over to a CDN but I want to easily be able to switch the CDN on or off without having to hard code the path to the images.

My first thought was to add an HttpHandler for image extensions that depending whether a variable in the web.config (something like ) will serve the image from the server or from the CDN. But after giving this a little though I think I've essentially ruled this out as it will cause ASP.NET to handle the request for every single image, thus adding overhead, and it might actually completely mitigate the benefits of using a CDN.

An alternative approach is, since all of my pages inherit from a base page class, I could create a function in the base class that determines what path to serve the files from based off the web.config variable. I would then do something like this in the markup:

<img src='<%= GetImagePath()/image.png' />

I think this is probably what I'll have to end up doing, but it seems a little clunky to me. I also envision problems with the old .NET error of not being able to modify the control collection because of the "<%=" though the "<%#" solution will probably work.

Any thoughts or ideas on how to implement this?

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about cdn