css - 自定义字体在本地有效但在服务器上无效?

标签 css asp.net-mvc iis font-face truetype

我有一个@font-face在我网站的级联样式表中定义。当我浏览到本地站点(由 IIS Express 提供服务)时,此字体加载正常。但是,当从远程服务器 (IIS 7.5) 加载我的应用程序时,对此字体的引用会导致 internal server 500 error 。为什么下面的样式在本地有效,但在服务器上无效?

@font-face {
    font-family: DejaVu Sans Mono;
    src: url('../fonts/DejaVuSansMono.ttf');
}

header h1
{
    font-family: 'DejaVu Sans Mono';
    font-size: 3em;
    padding: 0;
    margin: 0;
}

最佳答案

如果您检查,您会发现DejaVuSansMono.ttf 文件没有被复制到您的服务器。由于未复制该字体文件,因此 IIS 7.5 无法提供该文件。它在本地工作,因为 IIS Express 能够在本地目录中找到该文件。

Visual Studio 无法识别的文件类型的 BuildAction Property默认设置为;字体文件就是这样一种无法识别的类型。 BuildAction 为 none 意味着 Visual Studio 在构建应用程序时将忽略此文件。 BuildAction 属性有几个开箱即用的选项:

  • None - The file is not included in the project output group and is not compiled in the build process. An example is a text file that contains documentation, such as a Readme file.
  • Compile - The file is compiled into the build output. This setting is used for code files.
  • Content - The file is not compiled, but is included in the Content output group. For example, this setting is the default value for an .htm or other kind of Web file.
  • Embedded Resource - This file is embedded in the main project build output as a DLL or executable. It is typically used for resource files.

DejaVuSansMono.ttf 最合适的 BuildAction 属性设置是内容。通过以这种方式设置 BuildAction,文件将被复制到远程服务器。不幸的是,必须对每个具有无法识别的文件类型的文件重复此手动过程,这使开发人员面临一个繁琐且容易出错的过程。值得庆幸的是,Andre Loker 在 setting the default build action for non-default file-types in Visual Studio 上发表了一篇精彩的博客文章。 .

关于css - 自定义字体在本地有效但在服务器上无效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11371694/

相关文章:

c# - 具有派生类的 Controller 操作

c# - 检查 IIS 应用程序池的状态

c# - 从 C# 添加 Windows 功能

html - 为什么我的按钮没有出现在页面上?

javascript - watch 在 angularJs 中的指令链接内不起作用

css - 带空格的字体需要用引号括起来吗?

html - 鼠标悬停时,CSS 按钮没有响应

c# - 如何提高 DataSet 到 List<T> 的性能?

asp.net - 在 ASP.NET 中更改 session 变量并重新加载页面

c# - 中止请求时 IIS/C# 中会发生什么