CssRewriteUrlTransform is not being called

强颜欢笑 提交于 2019-11-27 02:04:46

问题


I just created a new MVC 5 app on VS 2013 RTM. For some reason background image url in my CSS files were not being transformed.

So, to debug the issue, I created my custom CssRewriteUrlTransform wrapper. And I found that my breakpoint is not being called.

This is what I have in my BundleConfig.cs

using System.Web.Optimization;

namespace Utilities.Web
{
    public class BundleConfig
    {
        private const string JQUERY_CDN_URL = "//code.jquery.com/jquery-1.10.1.min.js";

        public static void RegisterBundles(BundleCollection bundles)
        {
            bundles.UseCdn = true;
            BundleTable.EnableOptimizations = true;

            bundles.Add(new StyleBundle("~/css/coming-soon")
                .Include("~/Content/Site/coming-soon.css",
                    new CssRewriteUrlTransformWrapper()));

            bundles.Add(new ScriptBundle("~/js/coming-soon")
                .Include("~/Scripts/jquery.placeholder.js")
                .Include("~/Scripts/Site/coming-soon.js"));

            bundles.Add(new ScriptBundle("~/js/jquery", JQUERY_CDN_URL)
            {
                CdnFallbackExpression = "window.jQuery"
            }.Include("~/Scripts/jquery-{version}.js"));
        }
    }

    public class CssRewriteUrlTransformWrapper : IItemTransform
    {
        public string Process(string includedVirtualPath, string input)
        {
            return new CssRewriteUrlTransform().Process(includedVirtualPath, input);
        }
    }
}

回答1:


It appears the transform does not run if you have the minified version of the CSS. Remove the .min.css file and it should start working.




回答2:


I would have liked to put this as a comment under the marked answer. But I do not have the right to do so. The answer helped me. I also found another solution for it. In the Bundle-configuration method add this:

  • BundleTable.Bundles.FileExtensionReplacementList.Clear();

This will avoid the *.min.css file to be included automatically. And the transform will be called.

Regards Hans




回答3:


I have the same problem. Сlass CssRewriteUrlTransform does not work as I need. I looked at the source code AspNetWebOptimization and found that when Bundle finds a file with ".min", it creates a new BundleFile without transforms from the original BundleFile. The best solution is to turn off the FileExtensionReplacement for these bundles:

var bundle = new StyleBundle("~/bundles/css/font-awesome")
    .Include("~/Content/font-awesome.css", new CssRewriteUrlTransform());
bundle.EnableFileExtensionReplacements = false;
bundles.Add(bundle);


来源:https://stackoverflow.com/questions/19619086/cssrewriteurltransform-is-not-being-called

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!