asp.net mvc cssRewriteUrlTransform多个参数

dux*_*x-- 11 css asp.net asp.net-mvc

我正在尝试在bundleconfig中的一个bundle中使用CssRwriteUrlTransform,但我一直得到一个缺少的参数错误,这就是我所拥有的:

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",
));
Run Code Online (Sandbox Code Playgroud)

这可能是错的,但我不知道在哪里添加带有多个参数的include的CssRewriteUrlTransform参数

hai*_*770 26

你不能混合Include方法的两个重载:

public virtual Bundle Include(params string[] virtualPaths);
public virtual Bundle Include(string virtualPath, params IItemTransform[] transforms);
Run Code Online (Sandbox Code Playgroud)

如果您需要CssRewriteUrlTransform每个文件,请尝试以下方法:

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())
);
Run Code Online (Sandbox Code Playgroud)


Sim*_*mon 10

我遇到了同样的情况,最终创建了一个小扩展方法:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

然后你可以像这样调用它:

        bundles.Add(new StyleBundle("~/bundles/foo").IncludeWithCssRewriteUrlTransform(
            "~/content/foo1.css",
            "~/content/foo2.css",
            "~/content/foo3.css"
        ));
Run Code Online (Sandbox Code Playgroud)