web.config中有关媒体类型(MIME)的设置

164 阅读1分钟

IIS中,有些文件类型并不天然支持,比如.json文件。直接通过浏览器访问,会报404错误。除此而外,有些字体文件,比如.woff之类,也都类似。

这个问题,可以在IIS上直接设置来解决。
在这里插入图片描述
但这不是最好的办法。如果部署在多台机器,每一台机器的IIS都要设置,比较繁琐。

第二个就是在asp.net项目的web.config里直接设置:

  <system.webServer>
    <staticContent>
	  <remove fileExtension=".woff" />
	  <mimeMap fileExtension=".woff" mimeType="application/x-font-woff" />
	  <remove fileExtension=".woff2" />
      <mimeMap fileExtension=".woff2" mimeType="application/x-font-woff" />	  	
      <remove fileExtension=".json" />
      <mimeMap fileExtension=".json" mimeType="application/json" />
      <remove fileExtension=".ejs" />
      <mimeMap fileExtension=".ejs" mimeType="text/plain" />
    </staticContent>
  </system.webServer>

注意这个<remove>。务必要加上。否则IIS上已经进行了该类媒体类型的设置,则因为冲突而报错。