[Azure][ASP.NET]woff,woff2,ttf,svg檔案無法正常回應(404 Not Found)

  • 2135
  • 0
  • 2018-03-06

當網站專案佈署至Azure Web Service時可能會遇上部分副檔名的檔案會回傳404 (Not Found)的處理方法

Web.config加入需要的副檔名敘述

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

 

IIS Express偵錯調整

如果專案正在使用IIS Express進行開發,可能反而造成偵錯時發生404 (Not Found)

原因是IIS Express 的預設staticContent支援度更廣,手動加入的mimeMap與預設的mimeMap項目重複造成衝突(fileExtension是唯一key)

IIS Express設定檔位置:%USERPROFILE%\documents\iisexpress\config\applicationhost.config

避免與未知的預設設定發生key衝突可以先移除要被加入的fileExtension key

Web.config設定如下:

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

總結

Local開發(IIS Express) & 雲端佈署(Azure App Service)都能正常回應特殊副檔名的檔案。

也可以選擇拆分Web.Debug.config 、 Web.Release.config