29

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);
        }
    }
}
tereško
  • 58,060
  • 25
  • 98
  • 150
Moon
  • 33,439
  • 20
  • 81
  • 132

3 Answers3

72

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.

Randall Sutton
  • 1,875
  • 19
  • 27
  • 11
    Alternatively, you can reference the minified version directly and the transform appears to run correctly. – Chris Jan 28 '14 at 04:08
  • 4
    Thanks Randall, I can't believe how much time I wasted over this :) – Richard Feb 27 '14 at 15:31
  • This worked for me, and shouldn't have done. Is there somewhere we can log a bug report? – Rob Lyndon Feb 16 '15 at 13:25
  • @Chris Alternatively, MS could have fixed this obvious bug. – A.R. Mar 07 '18 at 22:39
  • OMG. I had a missing glyphicon issue for so long and I only ever got as far as here https://stackoverflow.com/questions/31211545/using-glyphicons-in-an-asp-net-mvc-app-that-has-bootstrap-bundled and gave up when trannsform didn't work. I can't believe adding .min fixed this. Completely crazy. – Nick.Mc Apr 23 '19 at 13:07
6

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);
Alieh S
  • 170
  • 2
  • 8
  • I tried to remove the *.min.css but it still lingers on my azure. But this solution actually saves me from manually deleting the file. – Sam Aug 06 '20 at 15:36
5

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

Hans Kindberg
  • 510
  • 5
  • 10
  • Seems insane to me that this behavior was implemented in the first place. I can't count how many times I got bitten by these .min files for various reasons. Your solution is a bit extreme (it clears the complete list instead of removing just the .min files), but actually it seems there is no public API to remove just one element... So it's the best way to solve the problem I've seen so far. – youen Feb 02 '17 at 10:53