【问题标题】:ASP.NET Core 2 MVC - Font-awesome woff/2 ttf not loading in productionASP.NET Core 2 MVC - 字体真棒 woff/2 ttf 未在生产中加载
【发布时间】:2018-04-21 21:09:46
【问题描述】:

我正在构建一个个人项目,但遇到了一些字体很棒的网络字体无法加载的问题。我做了一个 gulp 构建来集成 Bootstrap 4 并将我所有的 JS/SCSS/Fonts/Img 从 src 目录构建到 dist 目录。我在 wwwroot 之外为 Dist 服务

将 ASPNETCORE_ENVIRONMENT 设置为开发时,一切正常,我的字体正确加载,但在“生产”中,它没有加载,我在控制台中收到以下消息:

Failed to load resource: the server responded with a status of 404 (Not Found)fa-regular-400.woff
Failed to load resource: the server responded with a status of 404 (Not Found)fa-regular-400.woff2
Failed to load resource: the server responded with a status of 404 (Not Found) fa-regular-400.ttf

当我将鼠标悬停在错误上时,我看到它们是从我的 SrcAssets 提供的,而不是像我的所有其他资产文件一样的 DistAssets。

我没有找到任何关于 Core2.0 MVC 的可用资源,我发现的所有内容都是关于需要将这些文件扩展名映射到的旧东西。应用程序/font-woff2

我尝试过的最后一件事是在 Configure 方法中,但它仍然不起作用。

FileExtensionContentTypeProvider typeProvider = new FileExtensionContentTypeProvider();

if (!typeProvider.Mappings.ContainsKey(".woff2"))
{
    typeProvider.Mappings.Add(".woff2", "application/font-woff2");
}
if (!typeProvider.Mappings.ContainsKey(".woff"))
{
    typeProvider.Mappings.Add(".woff", "application/font-woff");
}
if (!typeProvider.Mappings.ContainsKey(".ttf"))
{
    typeProvider.Mappings.Add(".woff", "application/font-ttf");
}

app.UseStaticFiles(new StaticFileOptions
{
    ContentTypeProvider = typeProvider,
    FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "DistAssets")),
    RequestPath = "/assets"
});

感谢您的帮助。

【问题讨论】:

    标签: asp.net-core-mvc asp.net-core-mvc-2.0


    【解决方案1】:

    我遇到了同样的问题,但我在 _variables.scss 中添加了 "../lib/FontAwesome/webfonts" 而不是 "../webfonts"

    $fa-font-path:                "../lib/FontAwesome/webfonts" !default;
    

    也许它也会帮助你:)

    【讨论】:

      【解决方案2】:

      尝试将此代码添加到您的 web.config 中

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

      【讨论】:

        猜你喜欢
        • 2013-10-16
        • 2014-09-07
        • 1970-01-01
        • 2017-06-25
        • 2016-10-20
        • 2023-04-09
        • 2018-11-05
        • 2020-08-01
        • 2016-09-29
        相关资源
        最近更新 更多