3

This is a very straight forward question.

I have a Content-Type stored in the form of a string.

Ideally I'd like to infer an extension from that Content-Type without having to have a giant nasty switch case.

Is there a built in construct to achieve this?

Btw, I found this question but that goes the opposite direction from extension to content-type.

Community
  • 1
  • 1
Mike Fielden
  • 10,055
  • 14
  • 59
  • 99
  • Possible duplicate of [C# Get file extension by content type](https://stackoverflow.com/questions/23087808/c-sharp-get-file-extension-by-content-type) – Marcell Toth May 20 '19 at 11:24

2 Answers2

4

You'll want a Dictionary. This will allow you to look up an extension for a given content type:

Dictionary<string, string> extensionLookup = new Dictionary<string, string>()
{
    {"ContentType1", ".ext1"},
    {"ContentType2", ".ext2"},
};

You can populate the dictionary based on a database table, a file, etc. rather than hard coding the values.

Once you have the Dictionary it's as simple as:

string extension = extensionLookup[someContentType];
Servy
  • 202,030
  • 26
  • 332
  • 449
  • 1
    This assumes I know all the possible extensions ahead of time though. And as you know that is almost impossible, right? – Mike Fielden Jan 23 '13 at 16:02
  • @MikeFielden It makes no such assumption: `"You can populate the dictionary based on a database table, a file, etc. rather than hard coding the values."` You'll need to get the mappings from *somewhere*, but you can get those mappings at runtime and add them to the dictionary without knowing what they are at compile time. – Servy Jan 23 '13 at 16:02
  • No, I understand what youre saying I was just hoping there was something like Apache's Tika :) for IIS – Mike Fielden Jan 23 '13 at 16:32
  • The reverse is possible in .NET 4.5, by calling System.Web.MimeMapping.GetMimeMapping(string fileName). I wish there was a way to get the proper file extention for a given content type from IIS as well. – Triynko Nov 18 '15 at 21:35
1

In the registry of my W10 box there is an extensive list mapping content-type to .extn:

[HKEY_CLASSES_ROOT\MIME\Database\Content Type]

You might like to explore the HtmlAgilityPack OSS project which has source file HtmlWeb.cs containing the following methods:

public static string GetContentTypeForExtension(string extension, string def)
public static string GetExtensionForContentType(string contentType, string def)

There is also [commented-out] Dictionary and population code:

//private static Dictionary<string, string> _mimeTypes;
//        _mimeTypes = new Dictionary<string, string>();
//        _mimeTypes.Add(".3dm", "x-world/x-3dmf");
//        _mimeTypes.Add(".3dmf", "x-world/x-3dmf");
//    ...
//        _mimeTypes.Add(".zoo", "application/octet-stream");
//        _mimeTypes.Add(".zsh", "text/x-script.zsh");

So you may brew your own (maybe just the common ones that YOUR app needs) for efficiency.

FYI the authoritative list of content-type values and usage is here: www.iana.org/assignments/media-types/media-types.xhtml

David Ferenczy Rogožan
  • 23,966
  • 9
  • 79
  • 68
Dick Baker
  • 71
  • 2