asp.net mvc cssRewriteUrlTransform多个参数

时间:2014-12-03 17:10:02

标签: css asp.net asp.net-mvc

我试图在bundleconfig中的一个捆绑包中使用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",
));

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

2 个答案:

答案 0 :(得分:26)

您无法混合Include方法的两种重载:

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

如果您需要每个文件都有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())
);

答案 1 :(得分: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;
    }
}

然后你可以这样称呼它:

        bundles.Add(new StyleBundle("~/bundles/foo").IncludeWithCssRewriteUrlTransform(
            "~/content/foo1.css",
            "~/content/foo2.css",
            "~/content/foo3.css"
        ));