Search code examples
c#asp.netimagednshttpmodule

add domain to images source at runtime


I have a number of images on my website (asp.net c# web.application) that are loaded on to my site. I have a specific domain that I use to load images with but I dont have it included in my markup. So at runtime I want to add this domain to the images if they dont already have it added at compile time. Whats the best way of doing this? Is it via a http module? I understand by doing this at runtime will have performance issues so if there are any other ways I am open to suggestions. Most of my images have relative paths. I cant hard code the domain as it changes by environment the application is running in.


Solution

  • Put the domain in your web.config's app settings, then at the top of the web form or master page, do:

    <% string ImagesDomain = System.Configuration.ConfigurationSettings.AppSettings["ImagesDomain"] %>
    

    Then in your images, do:

    <img src="<%=ImagesDomain%>/MyImage.jpg" />
    

    Simple enough. Don't worry too much about the performance hit either, it'll be fine.