【问题标题】:google font api not working asp.net mvc bundleconfig谷歌字体 api 不起作用 asp.net mvc bundleconfig
【发布时间】:2018-07-12 17:26:30
【问题描述】:

我目前正在开发一个 ASP.NET MVC 应用程序,我正在尝试在我的 bundleconfig.cs 中包含一个 google 字体。

不幸的是,它没有按预期工作。我收到此错误:

网址“https://fonts.googleapis.com/css?family=Rokkitt:200,300,400,700”无效。仅允许应用程序相对 URL (~/url)。 参数名称:虚拟路径

我当前在 BundleConfig.cs 的 RegisterBundles 方法中的编码如下所示:

    string[] libraryCssList = new string[] 
    {
       "~/Content/css1.css",
       "~/Content/css2.css",
    }; // works fine

    // error begins:

bundles.UseCdn = true;

    var fontRokkittCss = "https://fonts.googleapis.com/css?family=Rokkitt:200,300,400,700";

    bundles.Add(new StyleBundle("~/Content/libraryCss")
    .Include(libraryCss)
    .Include(fontRokkittCss));

你知道如何在 asp.net mvc 4 中将 google font api 包含到 bundle config 中吗?

谢谢!!

【问题讨论】:

  • 你可以保存css文件并将其包含在你的项目中吗?
  • 我相信只有本地文件可以捆绑在一起。尝试为字体创建一个单独的包。
  • 你是在调试模式下运行它吗?
  • 尝试捆绑这个的原因是什么?您不想在页面布局的链接块中引用字体吗?你可以通过他们的 fonts.gstatic cdn
  • 类似问题:stackoverflow.com/questions/13682671/absolute-url-in-asp-bundle。我认为最好包含本地副本而不是依赖 CDN(并非所有 CDN URL 都可以解析,它必须包含实际文件的路径而不是查询字符串作为参考)。

标签: c# css asp.net asp.net-mvc asp.net-mvc-4


【解决方案1】:

在RegisterBundles 方法中,必须将BundleCollection.UseCDN 成员变量设置为true。下面的例子,注意 Include 方法的使用(和不使用):

public static void RegisterBundles(BundleCollection bundles) {
    bundles.UseCdn = true;

    // SCRIPTS
    const string boostrapCdnPath = "https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/5.0.0-beta3/js/bootstrap.bundle.min.js";
    bundles.Add(new ScriptBundle("~/bundles/bootstrap.bundle.min.js", boostrapCdnPath));
    bundles.Add(new ScriptBundle("~/bundles/someLocalScript").Include("~/scripts/someLocalScript.js"));

    // STYLES
    bundles.Add(new StyleBundle("~/bundles/GoogleOpenSans", "https://fonts.googleapis.com/css2?family=Open+Sans:ital,wght@0,400;0,600;0,700;1,400;1,600;1,700&display=swap"));
    bundles.Add(new StyleBundle("~/bundles/bootstrap.min.css", "https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/5.0.0-beta3/css/bootstrap.min.css"));
    bundles.Add(new StyleBundle("~/bundles/bootstrap-icons.min.css", "https://cdnjs.cloudflare.com/ajax/libs/bootstrap-icons/1.4.0/font/bootstrap-icons.min.css"));
}

【讨论】:

  • 请注意,StyleBundle 方法的第一个参数的结尾似乎可以随意命名(例如 ~/bundles/anyNameYouWantHere),可以带或不带“文件扩展名”。但是,如果您确实添加了“文件扩展名”,则不能在本地样式上添加 .css 后缀。它会导致 404。我认为这是 ASP.NET 的一个错误,但需要进一步研究才能确定。
猜你喜欢
  • 2015-09-28
  • 1970-01-01
  • 2015-09-24
  • 2016-11-03
  • 2020-05-24
  • 2015-04-10
  • 2012-05-04
  • 2017-12-10
相关资源
最近更新 更多