在一个StyleBundle中使用多个CDN链接

时间:2012-10-03 19:39:05

标签: asp.net-mvc-4

我有以下代码:

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

是否有其他人设法解决此问题或设法扩展允许此行为的框架?

2 个答案:

答案 0 :(得分:0)

你可以使用

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

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

答案 1 :(得分:0)

我认为这仍处于开发阶段,请查看最新的SO问题: Absolute URL in ASP bundle