Get extension from Content-Type

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.

Answers


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];

Need Your Help

Custom webserver caching

http caching http-headers webserver embeddedwebserver

I'm working with a custom webserver on an embedded system and having some problems correctly setting my HTTP Headers for caching.

Android SwitchCase inside onListItemClick

android listview switch-statement

I am trying use a switch case. But it says i cant pass a string :(

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.