Recently, I needed to implement a minimal web server in C#. The .NET HTTP classes worked great, but there’s nothing built in to provide MIME types (other than looking in the registry, which I don’t trust to be complete/consistent). So I extracted the data from the Debian /etc/mime.types file for anything with a file extension and created a simple static class to do lookups for them.

Usage examples:

// do a lookup
String mimeType = MimeType.Get(".jpg");

// the dot is optional
String mimeType = MimeType.Get("jpg");

// non-existent extensions will return the default application/octet-stream type
String mimeType = MimeType.Get(".foo");

// you can also provide an explicit default type
String mimeType = MimeType.Get(".foo", "text/plain");

You can download MimeType.cs or view it in-line below.

Continue reading