Prepend CDN url to mvc 4 bundler output

后端 未结 2 1531
执念已碎
执念已碎 2021-01-31 22:57

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

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-31 23:37

    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("
    
                                     
                  
提交回复
热议问题