Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asp.net mvc cssRewriteUrlTransform multiple arguments

I'm trying to use CssRwriteUrlTransform in one of my bundles in bundleconfig, but I keep getting a missing argument error, this is what I have:

bundles.Add(new StyleBundle("~/Content/GipStyleCss").Include(
       new CssRewriteUrlTransform(),
       "~/Content/GipStyles/all.css",
       "~/Content/GipStyles/normalize.css",
       "~/Content/GipStyles/reset.css",
       "~/Content/GipStyles/style.css",
));

this is probably wrong, but I don't know where to add the the CssRewriteUrlTransform argument with an include that has multiple arguments

like image 701
duxfox-- Avatar asked Dec 03 '14 17:12

duxfox--


2 Answers

I ran into the same situation and ended up creating a small extension method:

public static class BundleExtensions {

    /// <summary>
    /// Applies the CssRewriteUrlTransform to every path in the array.
    /// </summary>      
    public static Bundle IncludeWithCssRewriteUrlTransform(this Bundle bundle, params string[] virtualPaths) {
        //Ensure we add CssRewriteUrlTransform to turn relative paths (to images, etc.) in the CSS files into absolute paths.
        //Otherwise, you end up with 404s as the bundle paths will cause the relative paths to be off and not reach the static files.

        if ((virtualPaths != null) && (virtualPaths.Any())) {
            virtualPaths.ToList().ForEach(path => {
                bundle.Include(path, new CssRewriteUrlTransform());
            });
        }

        return bundle;
    }
}

You can then call it like this:

        bundles.Add(new StyleBundle("~/bundles/foo").IncludeWithCssRewriteUrlTransform(
            "~/content/foo1.css",
            "~/content/foo2.css",
            "~/content/foo3.css"
        ));
like image 184
Simon Avatar answered Nov 13 '22 19:11

Simon


You can't mix both overloads of the Include method:

public virtual Bundle Include(params string[] virtualPaths);
public virtual Bundle Include(string virtualPath, params IItemTransform[] transforms);

If you need the CssRewriteUrlTransform on each of the files, try this:

bundles.Add(new StyleBundle("~/Content/GipStyleCss")
    .Include("~/Content/GipStyles/all.css", new CssRewriteUrlTransform())
    .Include("~/Content/GipStyles/normalize.css", new CssRewriteUrlTransform())
    .Include("~/Content/GipStyles/reset.css", new CssRewriteUrlTransform())
    .Include("~/Content/GipStyles/style.css", new CssRewriteUrlTransform())
);
like image 42
haim770 Avatar answered Nov 13 '22 20:11

haim770