File extensions and MIME Types in .NET

Posted by Marc Climent on Stack Overflow See other posts from Stack Overflow or by Marc Climent
Published on 2009-10-23T11:13:00Z Indexed on 2010/03/12 23:07 UTC
Read the original article Hit count: 561

I want to get a MIME Content-Type from a given extension (preferably without accessing the physical file). I have seen some questions about this and the methods described to perform this can be resumed in:

  1. Use registry information.
  2. Use urlmon.dll's FindMimeFromData.
  3. Use IIS information.
  4. Roll your own MIME mapping function. Based on this table, for example.

I've been using no.1 for some time but I realized that the information provided by the registry is not consistent and depends on the software installed on the machine. Some extensions, like .zip don't use to have a Content-Type specified.

Solution no.2 forces me to have the file on disk in order to read the first bytes, which is something slow but may get good results.

The third method is based on Directory Services and all that stuff, which is something I don't like much because I have to add COM references and I'm not sure it's consistent between IIS6 and IIS7. Also, I don't know the performance of this method.

Finally, I didn't want to use my own table but at the end seems the best option if I want a decent performance and consistency of the results between platforms (even Mono).

Do you think there's a better option than using my own table or one of other described methods are better? What's your experience?

© Stack Overflow or respective owner

Related posts about mime

Related posts about ASP.NET