---
title: "How to Transform Styles Urls to CDN Urls in ASP.NET Bundling and Minification"  
description: "How to Transform Styles Urls to CDN Urls in ASP.NET Bundling and Minification"  
author: "Anonymous User"  
published: 2015-01-06  
updated: 2015-01-06  
canonical: https://www.mindstick.com/forum/12842/how-to-transform-styles-urls-to-cdn-urls-in-asp-dot-net-bundling-and-minification  
category: "asp.net"  
tags: ["asp.net mvc"]  
reading_time: 2 minutes  

---

# How to Transform Styles Urls to CDN Urls in ASP.NET Bundling and Minification

Some of my styles use url(../img/sprites/main_sprite.png) to local resources in [development](https://www.mindstick.com/articles/65309/importance-of-ux-design-in-the-development-of-mobile-apps) and stage. However in [production](https://yourviews.mindstick.com/story/1489/the-countries-that-dominate-world-fossil-fuel-production) I use CDN and all my [static resources](https://www.mindstick.com/forum/158595/explain-the-concept-of-bundling-and-minification-in-asp-dot-net-mvc-for-managing-static-resources) are on it. Is it possible to [transform](https://www.mindstick.com/interview/511/what-are-the-steps-to-transform-xml-into-html-using-xsl) [bundles](https://www.mindstick.com/forum/33890/examples-of-intent-and-bundles) so that all urls in .css are replaced with cdn path?

**For Example:**

```
.Logo {background-image: url(../img/sprites/main_sprite.png);  }However, in production I would like it to be.Logo {background-image: url(http://MyCdn.com/img/sprites/main_sprite.png);    }I already use CssRewriteUrlTransform() to rewrite my relative paths to absolute, so the resources can be found after they bundled.I was thinking to extend the class as something like thispublic string Process(string includedVirtualPath, string input){     if (_useCdn)        {             return  new CssRewriteUrlTransform().Process(_cdn + VirtualPathUtility.ToAbsolute(includedVirtualPath), input);                                   }        else        {             return  new CssRewriteUrlTransform().Process("~" + VirtualPathUtility.ToAbsolute(includedVirtualPath), input);        }     }
```

However, [Process](https://yourviews.mindstick.com/story/1525/7-important-factors-that-may-affect-the-learning-process) must have VirtualPath, otherwise it throws an [exception](https://www.mindstick.com/articles/1824/objective-c-exception-handling) when I append CDN path.

Is there an equivalent of this [class](https://www.mindstick.com/blog/165/generic-class-in-c-sharp) to rewrite URLS with CDN in it?

## Replies

### Reply by Anonymous User

I was not able to find an existing solution. So, I used CssRewriteUrlTransform code as the base for my CDNStylesTransformer. I hope it will be useful for you too.

```
/// <summary>/// Finds and Replaces Urls with CDN links./// </summary>public class CDNStylesTransformer : IItemTransform{    private bool _useCdn;    private string _cdnBaseUrl;    public CDNStylesTransformer(bool
UseCDN, string CdnBaseUrl)    {        _useCdn = UseCDN;        if(CdnBaseUrl ==null || CdnBaseUrl.Equals(string.Empty))        {            throw new ArgumentNullException("CdnBaseUrl");        }        _cdnBaseUrl = CdnBaseUrl;     }     internal static string RebaseUrlToCDNUrl(string cdnUrl, string url)    {        // Don't do anything to invalid urls or absolute urls        if (String.IsNullOrWhiteSpace(url)||            String.IsNullOrWhiteSpace(url)||            url.StartsWith("data:")||               !VirtualPathUtility.IsAbsolute(url))        {            return url;        }         return cdnUrl +url;    }     internal static string ConvertUrlsToCDNUrl(string cdnUrl, string content)    {        if (String.IsNullOrWhiteSpace(content))        {            return content;        }        // Replace all urls with CDN urls        Regex url = new Regex(@"url\(['""]?(?<url>[^)]+?)['""]?\)");        return url.Replace(content,((match) =>        {            return "url("+ RebaseUrlToCDNUrl(cdnUrl, match.Groups["url"].Value) + ")";        }));    }     public  string Process(string includedVirtualPath, string input)    {        if (_useCdn)         {             return ConvertUrlsToCDNUrl(_cdnBaseUrl,input);        }        else        {            return input; //do nothing          }    }}
```

In your BundleConfiguration class

```
string cdnPath ="http://MyCdn.com";bundles.Add(new StyleBundle("~/Content/themes/base/css").Include(      "~/Content/themes/base/style1.css",
new CDNStylesTransformer(bundles.UseCdn,cdnPath)      ));
```


---

Original Source: https://www.mindstick.com/forum/12842/how-to-transform-styles-urls-to-cdn-urls-in-asp-dot-net-bundling-and-minification

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
