60

The closest thing I could find was System.Net.Mime.MediaTypeNames but that doesn't seem to have everything (like json) since it seems to be more focused around email attachments.

David d C e Freitas
  • 7,481
  • 4
  • 58
  • 67
will
  • 3,975
  • 6
  • 33
  • 48
  • 4
    Something similar [here](http://stackoverflow.com/questions/10362140/asp-mvc-are-there-any-constants-for-the-default-content-types). People usually deal with it by creating constants as `System.Net.Mime.MediaTypeNames` would never be an exhaustive/complete list. – RBT Jan 09 '17 at 06:42
  • Related post - [What is the correct JSON content type?](https://stackoverflow.com/q/477816/465053) – RBT Feb 28 '19 at 05:03

5 Answers5

32

An enum doesn't make much sense. MIME types are open-ended. That is, the list is not finite: new types are added from time to time.

See RFC4288: Media Type Specifications and Registration Procedures

Chris W. Rea
  • 5,430
  • 41
  • 58
  • 8
    It makes sense to me. Not as part of the .NET framework because that isn't updated frequently (nor should it be). But as a separate library that could be updated as frequently as new types are added (and users of the library cared). Time zones are open-ended too but that doesn't seem to obviate the utility of libraries based on [tz database](http://www.wikiwand.com/en/Tz_database). – Kenny Evitt Jan 08 '15 at 15:56
  • 3
    @KennyEvitt We keep an internal enum for common types we use a lot. Nothing wrong with that. Seems better than magic strings, right? – crush Jun 04 '15 at 20:55
  • Hindsight is 20/20 but MIME types, like HTTP status codes, whilst not finite, are a sensible use for constants or enums. As per my answer, in 2022, Microsoft include MIME type string constants. – Richard Feb 12 '23 at 17:36
9

In 2022, with .NET Core and .NET 5+, this is now available via MediaTypeNames. For example:

  • MediaTypeNames.Application.Json
  • MediaTypeNames.Image.Png
  • MediaTypeNames.Text.Html

Microsoft documentation around MediaTypeNames, and each of Application, Image, Text.

Update 2013: Further MIME types have been added in .NET 8, and are much more exaustive: https://learn.microsoft.com/en-us/dotnet/api/system.net.mime.mediatypenames?view=net-8.0

Richard
  • 434
  • 5
  • 9
  • Some of these mediatypenames also exist in .net 4.6, but important ones like application/json are missing – user2728841 Apr 27 '23 at 09:12
  • This is good, but it looks like it is quite limited. I was looking for `text/csv` but it isn't supported. – Michael Murphy May 04 '23 at 05:50
  • 1
    @MichaelMurphy It appears support for `text/csv` has been added in .NET 8, alongside a bunch of others! https://learn.microsoft.com/en-us/dotnet/api/system.net.mime.mediatypenames.text.csv?view=net-8.0#system-net-mime-mediatypenames-text-csv – Richard Aug 03 '23 at 20:40
  • 1
    @richard_ob it's about time! – Michael Murphy Aug 05 '23 at 18:12
3

IANA's database is most likely to be complete. Currently, they have the list available in CSV format at https://www.iana.org/assignments/media-types/application.csv. I am assuming this is a stable URL whose content changes as updates are made. If you want to stay up to date, you'd need to put together a mechanism that is appropriate for your needs.

There is also the mime.types file that comes with Apache which seems to have been derived from the said list.

Sinan Ünür
  • 116,958
  • 15
  • 196
  • 339
  • A flat list is already offered as CSV-file by the IANA's database website. – ˈvɔlə Dec 19 '13 at 13:55
  • 1
    I parsed the listed on Wikipedia some years ago and generated C#, its here: http://stackoverflow.com/questions/10362140/asp-mvc-are-there-any-constants-for-the-default-content-types – Luke Puplett Jan 15 '15 at 18:35
  • Here a link to the IANA database with working .csv links: https://www.iana.org/assignments/media-types/media-types.xhtml – Florian Falk Dec 14 '21 at 13:58
1

If like me you wanted to have no hard-coded string in your code you can use something like below

httpHeaders.add(HttpHeaders.CONTENT_TYPE,MediaType.APPLICATION_JSON_VALUE);

which is essentially httpHeaders.add("Content-Type","application/json");

1

For my own limited purposes, I have made a static class that is basically a copy of the included .NET MediaTypeNames class but includes the MIME types I require; .NET MediaTypeNames is missing values I need.

public static class MediaMimeTypes
{
    /// <summary>
    /// Specifies that the MediaMimeType is not interpreted.
    /// </summary>
    public const string Octet = "application/octet-stream";

    /// <summary>
    /// Specifies that the MediaMimeType is in a Comma Separated Values (CSV) format.
    /// </summary>
    public const string Csv = "text/csv";
}
Michael Murphy
  • 1,921
  • 2
  • 18
  • 21