Using the built in MVC4 bundler, how do I prepend my CDN url to the link tags it produces? I\'ve setup Amazon Cloudfront so that it pulls assets from my webserver when first re
I just setup MaxCDN and ran into the same exact issue.
As you know, the bundles.UseCdn
property is not ideal because we don't want to have to specify the exact url for the bundle. A CDN like Max CDN is the same exact url, query string and all, except for a different subdomain.
Here is how I ended up solving it.
I created a BundleHelper
class that will wrap the render method and then prepend the path with the CDN subdomain.
Here is what the class looks like:
namespace MyDomain.Web.Helpers
{
public class BundleHelper
{
public static string CdnPath = "http://cdn.mydomain.com";
public static IHtmlString RenderScript(string path)
{
var opt = System.Web.Optimization.Scripts.Render(path);
string htmlString = HttpUtility.HtmlDecode(opt.ToHtmlString());
if (BundleTable.EnableOptimizations)
{
htmlString = htmlString.Replace("
Then to use it in the views, I simply do:
@BundleHelper.RenderStyle("~/Content/css")
@BundleHelper.RenderStyle("~/Content/themes/base/css")
@BundleHelper.RenderScript("~/bundles/jquery")
@BundleHelper.RenderScript("~/bundles/jqueryui")
Hope this helps.