2012-07-24 72 views
7

我有一个带有显示外部页面的iframe的页面。外部页面配置为从我的服务器下载CSS文件。在ASP.NET中,如何为仅TTF文件添加CORS头?

在CSS,我加了@font-face选择:

@font-face { 
    font-family: "Special Font"; 
    src: url("<%= Request.Url.GetLeftPart(UriPartial.Authority) + "/fonts/specialfont.ttf" %>"); 
} 

此下载并显示在浏览器的字体精细,但在Firefox,它下载的字体,但拒绝使用它。做一点研究表明,这个问题是一个交叉来源的政策问题。一个在这里提到的解决方案:

http://enable-cors.org/

是使CORS头。然而,它提供的解决方案是网站范围:

<?xml version="1.0" encoding="utf-8"?> 
    <configuration> 
     <system.webServer> 
      <httpProtocol> 
       <customHeaders> 
        <add name="Access-Control-Allow-Origin" value="*" /> 
       </customHeaders> 
      </httpProtocol> 
     </system.webServer> 
</configuration> 

而我只想启用仅.TTF文件。有没有办法做到这一点,无论是通过使用HttpHandler或其他方法?

回答

8

通过使用web.config中的location element可以将配置规则限制到特定的目录或文件,您可以实现类似的目标。例如:

<configuration> 
    <location path="fonts/specialfont.ttf"> 
    <system.webServer> 
     <httpProtocol> 
      <customHeaders> 
      <add name="Access-Control-Allow-Origin" value="*" /> 
      </customHeaders> 
     </httpProtocol> 
    </system.webServer> 
    </location> 
</configuration> 

尽管您可能希望为所有字体启用CORS,例如: <location path="fonts">

this question的答案有location的更多示例。