asp.net mvc cssRewriteUrlTransform 多个参数
Posted
技术标签:
【中文标题】asp.net mvc cssRewriteUrlTransform 多个参数【英文标题】:asp.net mvc cssRewriteUrlTransform multiple arguments 【发布时间】: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 参数
【问题讨论】:
【参考方案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())
);
【讨论】:
以上是关于asp.net mvc cssRewriteUrlTransform 多个参数的主要内容,如果未能解决你的问题,请参考以下文章
七天学会ASP.NET MVC ——ASP.NET MVC 数据传递
ASP.NET MVC 5、ASP.NET Core MVC 5 有啥区别?