【问题标题】:asp.net mvc cssRewriteUrlTransform multiple argumentsasp.net mvc cssRewriteUrlTransform 多个参数
【发布时间】:2015-02-01 08:43:59
【问题描述】:

我正在尝试在 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 参数

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      您不能混合使用 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())
      );
      

      【讨论】:

        猜你喜欢
        • 2016-07-11
        • 2014-04-10
        • 1970-01-01
        • 1970-01-01
        • 2015-12-08
        • 1970-01-01
        • 2021-11-05
        • 2011-01-13
        • 1970-01-01
        相关资源
        最近更新 更多