我有以下代码:
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,600","~/Content/less/mylessoverrides.less","~/Content/css/font-awesome.css"); The URL 'http://fonts.googleapis.com/css?family=Open+Sans:400italic,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/css/font-awesome.css");
有没有人设法解决这个问题,还是设法扩展允许这种行为的框架?
解决方法
你可以使用
@import url("http://fonts.googleapis.com/css?family=Open+Sans:400italic,600");
你想要在本地的css文件中