我有以下代码:
bundles.UseCdn = true;
var fontPath = "http://fonts.googleapis.com/css?family=Open+Sans:400italic,600italic,400,600";
var commonCss = new StyleBundle("~/css/common").Include(
"~/Content/less/myless.less",
"~/Content/css/opensans.css", // local copy of the font path above
"~/Content/less/mylessoverrides.less",
"~/Content/css/font-awesome.css");
commonCss.Transforms.Add(new CssMinify());
bundles.Add(commonCss);
上面的代码工作得很好。但是,我想从上面的 CDN 链接加载 opensans.css。我还想将其保留为一个捆绑包并保持捆绑包顺序不变。以下代码将引发错误:
var commonCss = new StyleBundle("~/css/common").Include(
"~/Content/less/myless.less",
"http://fonts.googleapis.com/css?family=Open+Sans:400italic,600italic,400,600",
"~/Content/less/mylessoverrides.less",
"~/Content/css/font-awesome.css");
The URL 'http://fonts.googleapis.com/css?family=Open+Sans:400italic,600italic,400,600'
is not valid. Only application relative URLs (~/url) are allowed.
一种解决方法是将其拆分为三个捆绑包,但这与捆绑包的全部目的背道而驰:
var commonCss1 = new StyleBundle("~/css/common1").Include(
"~/Content/less/myless.less");
var commonCss2 = new StyleBundle("~/css/common2", fontPath).Include(
"~/Content/css/opensans.css");
var commonCss3 = new StyleBundle("~/css/common3").Include(
"~/Content/less/mylessoverrides.less",
"~/Content/css/font-awesome.css");
最终的解决方案是执行以下操作:
var commonCss = new StyleBundle("~/css/common").Include(
"~/Content/less/myless.less",
new { fontPath, "~/Content/css/opensans.css" },
"~/Content/less/mylessoverrides.less",
"~/Content/css/font-awesome.css");
有没有其他人设法解决这个问题或设法扩展允许这种行为的框架?