Users Pricing

forum

Home Forums How to Transform Styles Urls to CDN Urls in ASP.NET Bundling and Minification – MindStick

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

Anonymous User 2321 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?

 


1 Answers

Anonymous User 06 Jan 2015 Accepted Answer
Markdown for AI

A clean, structured version of this page for AI assistants and LLMs.

Open .md