ChatGPT解决这个技术问题 Extra ChatGPT

Add MIME mapping in web.config for IIS Express

I need to add a new MIME mapping for .woff file extensions to IIS Express.

If I add the following snippet to the "applicationhost.config" of IIS Express it works fine:

<staticContent lockAttributes="isDocFooterFileName">
    <mimeMap fileExtension=".woff" mimeType="font/x-woff" />
    ...

But I would actually like to do add it to my "web.config" so that not every developer would need to change their "applicationhost.config" locally.

So I removed it again from the "applicationhost.config" file and added the following snippet to the project's "web.config":

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

Unfortunately it doesn't seem to work that way because when I try to access a .woff file I end up with a HTTP 404.3 error.

What am I doing wrong?


C
Community

Putting it in the "web.config" works fine. The problem was that I got the MIME type wrong. Instead of font/x-woff or font/x-font-woff it must be application/font-woff:

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

See also this answer regarding the MIME type: https://stackoverflow.com/a/5142316/135441

Update 4/10/2013

Spec is now a recommendation and the MIME type is officially: application/font-woff


According to link, the updated mime type should now be: application/font-woff (rather than deprecated application/x-font-woff).
@longda Thanks for that hint! I updated the answer to reflect that.
Does this method of defining custom MIME types in the web.config only work in IIS Express? I've tried it in a web.config deployed to IIS 6 and it does not work (throws 404). If I add the MIME type via the IIS Manager Console, it does work.
@WalterStabosz The above works for IIS and IIS Express 7+. If you are on IIS 6 you need to configure mime-types on the server, see configuring mime-types on IIS 6.
H
Hawk

If anybody encounters this with errors like Error: cannot add duplicate collection entry of type ‘mimeMap’ with unique key attribute and/or other scripts stop working when doing this fix, it might help to remove it first like this:

<staticContent>
  <remove fileExtension=".woff" />
  <mimeMap fileExtension=".woff" mimeType="application/font-woff" />
</staticContent>

At least that solved my problem


Thanks, that adds some safety when deploying :)
Add this to my list of features I love about IIS.
If the server already contains a mime type, specifying it in the web.config probably causes the duplicate exception.
T
ThomasDC
<system.webServer>
     <staticContent>
      <remove fileExtension=".woff"/>
      <mimeMap fileExtension=".woff" mimeType="application/font-woff" />
      <mimeMap fileExtension=".woff2" mimeType="font/woff2" />
    </staticContent>
  </system.webServer>

J
JayC

I know this is an old question, but...

I was just noticing my instance of IISExpress wasn't serving woff files, so I wen't searching (Found this) and then found:

http://www.tomasmcguinness.com/2011/07/06/adding-support-for-svg-to-iis-express/

I suppose my install has support for SVG since I haven't had issue with that. But the instructions are trivially modifiable for woff:

Open a console application with administrator privilages.

Navigation to the IIS Express directory. This lives under Program Files or Program Files (x86)

Run the command: appcmd set config /section:staticContent /+[fileExtension='woff',mimeType='application/x-woff']

Solved my problem, and I didn't have to mess with some crummy config (like I had to to add support for the PUT and DELETE verbs). Yay!


D
Daman Singh

Thanks for this post. I got this worked for using mustache templates in my asp.net mvc project I used the following, and it worked for me.

<system.webServer>   
  <staticContent>
   <mimeMap fileExtension=".mustache" mimeType="text/html"/>
  </staticContent>
</system.WebServer>

N
Nathan Prather

I'm not using IIS Express but developing against my Local Full IIS 7.

So if anyone else get's here trying to do that, I had to add the mime type for woff via IIS Manager

Mime Types >> Click Add link on right and then enter Extension: .woff MIME type: application/font-woff


Thanks, this got me up to speed with getting a project running again. Things were ok with IIS7.5 with me, but I got Win8 and IIS8, and mappings in the site's web.config were being duplicated with the IIS configuration, causing errors for any static resource being requested. I removed the duplicates and things are running fine now.
T
Tarun Gupta

To solve the problem, double-click the "MIME Types" configuration option while having IIS root node selected in the left panel and click "Add..." link in the Actions panel on the right. This will bring up the following dialog. Add .woff file extension and specify "application/x-font-woff" as the corresponding MIME type:

https://i.stack.imgur.com/u7oGK.png

Follow same for woff2 with application/x-font-woff2


This works for IIS, but the question relates to IIS Express. Is there a way to access this setting for the IIS Express web server?
B
Brian Wirt

I was having a problem getting my ASP.NET 5.0/MVC 6 app to serve static binary file types or browse virtual directories. It looks like this is now done in Configure() at startup. See http://docs.asp.net/en/latest/fundamentals/static-files.html for a quick primer.