Is there an enum for the ContentType property on a HttpWebResponse ("text/plain", "application/octet-stream" etc.)?
Asked Answered
U

5

61

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.

Upcountry answered 19/6, 2009 at 0:35 Comment(2)
Something similar here. People usually deal with it by creating constants as System.Net.Mime.MediaTypeNames would never be an exhaustive/complete list.Impletion
Related post - What is the correct JSON content type?Impletion
A
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

Allpowerful answered 19/6, 2009 at 0:43 Comment(3)
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.Mortarboard
@KennyEvitt We keep an internal enum for common types we use a lot. Nothing wrong with that. Seems better than magic strings, right?Teutonize
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.Memo
M
14

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

Memo answered 20/1, 2023 at 21:30 Comment(5)
Some of these mediatypenames also exist in .net 4.6, but important ones like application/json are missingWisecrack
This is good, but it looks like it is quite limited. I was looking for text/csv but it isn't supported.Perky
@MichaelMurphy It appears support for text/csv has been added in .NET 8, alongside a bunch of others! learn.microsoft.com/en-us/dotnet/api/…Memo
@richard_ob it's about time!Perky
@Memo I think you mean Update 2023 (not 2013)! Also, thanks!Changteh
L
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.

Leiker answered 19/6, 2009 at 0:42 Comment(3)
A flat list is already offered as CSV-file by the IANA's database website.Dworman
I parsed the listed on Wikipedia some years ago and generated C#, its here: #10362640Calculate
Here a link to the IANA database with working .csv links: iana.org/assignments/media-types/media-types.xhtmlChere
S
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");

Schaub answered 23/5, 2022 at 14:24 Comment(0)
P
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";
}
Perky answered 4/5, 2023 at 6:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.