Get MIME Type by File Extension

I was searching for a way to get the MIME type of a file by file extension and .NET Framework 4.5 comes to the rescue.

You can make use of API GetMimeMapping which is found in the .NET Framework 4.5 System.Web.dll

Here is the documentation of it: MimeMapping.GetMimeMapping Method.

Example on how you can use it:

  1. string mimeType = System.Web.MimeMapping.GetMimeMapping(fileName);  
If you are running your ASP.NET web application in the integrated mode in IIS then this API actually uses the list of MIME types in the IIS and so seems to me that even if your IIS MIME types list gets upgraded, you dont need to update your code, .. Isn’t it?

Hope you find this useful.

Reference: Getting a MIME type from a file extension in ASP.NET 4.5.