Visual Studio 2013 gives "Cannot add duplicate collection entry of type ‘mimeMap’"
I have a site that was built using Visual Studio 2010. When I upgraded to Visual Studio 2013, on some pages I would get an error that said:
Cannot add duplicate collection entry of type 'mimeMap' with unique key attribute 'fileExtension' set to '.mp4'.
After some searching, I found a helpful post on a blog, but since the issue was a little different and I didn't find it on stackoverflow, I thought I'd post the question and answer here.
Solution 1:
The issue was that when I switched to Visual Studio 2013, the web server used for debugging changed. Visual Studio 2013 uses IIS Express by default. Although Visual Studio 2010 SP1 supports IIS Express, my installation was still using the default Visual Studio Development server.
Like IIS 7, Visual Studio Development Server did not define the mp4 mime type by default, so that's why we had it explicitly added in the web.config, like this:
<system.webServer>
<staticContent>
<mimeMap fileExtension=".mp4" mimeType="video/mp4" />
</staticContent>
</system.webServer>
IIS Express, on the other hand, is based on IIS 8, and IIS 8 defines the mp4 mime type, and many others, by default. So when the mimeMap is explicitly set in the web.config, it is considered a duplicate.
If you DO NOT need to support IIS 7 in your production environment, then you can just completely remove the mimeMap tag from the web.config. If you need to support BOTH IIS 7 and IIS 8, then you can use a remove tag for the mimeMap first and then set it again, like this:
<system.webServer>
<staticContent>
<remove fileExtension=".mp4" />
<mimeMap fileExtension=".mp4" mimeType="video/mp4" />
</staticContent>
</system.webServer>
In IIS 7 the removal will do nothing since it isn't already defined, but in IIS 8 it will remove the original so that the new one is not a duplicate. Thanks to Oliver Payen for his post on the IIS 7 and IIS 8 difference and the remove solution.
Solution 2:
Simply remove extension before add it.
<remove fileExtension=".mp4" />
<mimeMap fileExtension=".mp4" mimeType="video/mp4" />