【问题标题】:How can i apply woff fonts in Azure for .NEt core app?如何在 Azure 中为 .NET 核心应用程序应用 woff 字体?
【发布时间】:2019-01-10 12:02:30
【问题描述】:

我有一个 .Net Core MVC 应用程序,它使用包含 libwkhtmltox.dll 的 DinkToPdf 从 HTML 创建 PDF 文件。我使用的字体之一是 .woff 字体,这在我的本地机器上运行良好。但是在 Azure 中,不应用该字体。

我已经阅读了其他线程,指出应该将 mimeTypes 添加到 .Net Core 的 FileExtensionContentTypeProvider 中,而不是 .Net 应用程序中的 web.config。

var provider = new FileExtensionContentTypeProvider();
        provider.Mappings[".woff"] = "font/woff";
        provider.Mappings[".ttf"] = "font/ttf";
        provider.Mappings[".otf"] = "font/otf";
        app.UseStaticFiles(new StaticFileOptions
        {
            FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "fonts")),
            RequestPath = "/fonts",
            ContentTypeProvider = provider
        });

我也尝试过不同的 mimeType,但相信这些都是正确的。您可以看到我还添加了 .ttf 和 .otf 以查看 Azure 是否存在其中一个问题而不是另一个问题。然后我在 css 中引用字体,如下所示...

@font-face {
font-family: 'barcode';
src: url(../fonts/fre3of9x.woff) format('woff'), url(../fonts/fre3of9x.ttf) format('ttf'), url(../fonts/CODE39.otf) format('otf');
font-weight: normal;
font-style: normal; }

我在 Azure 中看不到任何我可以设置的东西,所以我碰壁了。有人可以帮忙吗?

【问题讨论】:

  • 我不知道这是否能解决您的问题,但请尝试右键单击解决方案资源管理器中的 woff 文件 -> 属性 -> 复制到输出目录,设置为“始终复制”。 azure 中可能缺少该文件。您是如何部署应用的?
  • 我可以在 wwwroot 文件夹下的字体文件夹下看到 azure 中的文件。部署会自动包含我相信的这个目录中的所有内容。
  • Azure 上的路径是否不正确?在你的 CSS 中你有 ../fonts/,尝试不使用 ../
  • 是的,然后它甚至不显示文本。

标签: c# azure fonts


【解决方案1】:

我假设您正在使用的 Azure 应用服务基本上只是 IIS。 IIS 不支持开箱即用的 WOFF。这将要求您将 web.config 添加到项目的根目录,并确保将其部署到应用服务。这是 web.config 中需要的内容。

<configuration>
  <system.webServer>
    <staticContent>
      <mimeMap fileExtension=".woff" mimeType="application/font-woff" />
      <mimeMap fileExtension=".woff2" mimeType="application/font-woff" /> 
    </staticContent>
  </system.webServer>
</configuration>

希望这会有所帮助。

您可能必须明确删除扩展并重新添加它们。这是我在几个我知道有效的网站中使用的。

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <staticContent>
    <remove fileExtension=".woff" />
    <remove fileExtension=".woff2" />
    <mimeMap fileExtension=".woff" mimeType="application/font-woff" />
    <mimeMap fileExtension=".woff2" mimeType="application/font-woff" />
  </staticContent>
  </system.webServer>
</configuration>

【讨论】:

  • 因为这是 .Net Core,所以 web config 是一个 json 文件,有点不同。我不确定它是否处理相同的内容?你知道吗?
  • 啊,我没有意识到 web.config 文件实际上已部署到 Azure,即使不是在本地使用。但我已经添加了 staticContent 部分,但仍然没有应用字体。
  • 这很有趣,您是否重新启动了应用服务。就 web.config 而言,如果您想要相同的行为,您甚至必须为 Python 项目使用一个。最终结束,除非您使用的是 Linux/Container 托管应用服务,否则它仍然是 IIS。
  • 更改 web.config 会重新启动应用程序,所以是的,我已经尝试过了。我再次尝试了不同的 mime 类型,因为有些文章有 application/font-woff,有些有 font/woff,但无济于事。所以现在 web.config 有... system.webServer>
  • 你的 web.config 在根目录吗?
猜你喜欢
  • 2019-04-17
  • 1970-01-01
  • 2023-02-23
  • 2017-05-12
  • 2020-02-15
  • 2020-05-29
  • 2016-07-01
  • 2018-08-21
  • 2017-03-22
相关资源
最近更新 更多