2014-12-03 63 views
9

我想在我的bundleconfig束之一來使用CssRwriteUrlTransform,但我不斷收到一個缺少參數錯誤,這就是我:asp.net的MVC cssRewriteUrlTransform多個參數

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參數包括具有多個參數

回答

23

不能混淆的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()) 
); 
+0

我明白了,謝謝,我現在就試試吧! – 2014-12-03 17:29:07

7

我遇到了同樣的情況,並最終創建一個較小的擴展方法:

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" 
     ));