Users Pricing

forum

home / developersection / forums / how to transform styles urls to cdn urls in asp.net bundling and minification

How to Transform Styles Urls to CDN Urls in ASP.NET Bundling and Minification

Anonymous User 2198 06 Jan 2015

Some of my styles use url(../img/sprites/main_sprite.png) to local resources in development and stage. However in production I use CDN and all my static resources are on it. Is it possible to transform 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 this
public 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 must have VirtualPath, otherwise it throws an exception when I append CDN path.

Is there an equivalent of this class to rewrite URLS with CDN in it?

 


I am a content writter !


1 Answers

Anonymous User 06 Jan 2015 Accepted Answer