13

我有以下代码:

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

有没有其他人设法解决这个问题或设法扩展允许这种行为的框架?

4

2 回答 2

0

你可以使用

@import url("http://fonts.googleapis.com/css?family=Open+Sans:400italic,600italic,400,600");

您希望它在本地 css 文件中的位置

于 2013-01-05T03:53:43.030 回答
0

我认为这仍在开发中,请查看最近的 SO 问题: ASP 捆绑中的绝对 URL

于 2013-02-07T00:40:35.660 回答