IIS7 Error: cannot add duplicate collection entry of type ‘mimeMap’ with unique key attribute ‘fileExtension’ . . .


For the sake of comparison, in IIS6 (Windows 2003) if you add a mime type (example: .xyz - xml/text) at the virtual directory level and then also attempt to adds the same mime type at the web site level, IIS6 will alert you that a child entity has an override for the mime type.  If I accept the override, all entities under the web site will inherit the mime type, but the one with an explicit value will override the inherited one. 

By contrast, in IIS7 (Windows 2008 and Vista), adding the same mime types at the application level (which IIS will place into a web.config file) and the site level (which IIS will place into the applicationHost.config file) may cause a conflict.  No warning is given except for an error in the client browser that goes something like this:

Server Error in Application
Internet Information Services 7.0
HTTP Error 500.19 - Internal Server Error

MIME Types
There was an error while performing this operation.
Line number: 25
Error: cannot add duplicate collection entry of type ‘mimeMap’ with unique key attribute ‘fileExtension’ set to ‘.xyz’



One solution would be to simply remove one of the two conflicting mimeMap extensions from one of the config files.  However, in some rare scenarios the IIS administator may need to not remove one of the two conflicting mime mappings.  If both mimeMaps are needed, the way to get around the conflict is to make one simple addition to the web.config which will do a REMOVE immediately prior to doing an ADD.

So if your application installation adds the following line to the web.config. . .

           <mimeMap fileExtension=".xyz" mimeType="xml/text" />

… And this line creates a conflict with the same mime map in the applicationhost.config file, all you have to do is add the following line ABOVE that line so that it will look like the following:

           <remove fileExtension=".xyz" />
           <mimeMap fileExtension=".xyz" mimeType="xml/text" />
Doing the remove before the add should be fine even when the .xyz mime map doesn't exist at a higher level.