I want to get a MIME Content-Type from a given extension (preferably without accessing the physical file). I have seen some questions about this and the methods described to perform this can be resumed in:
- Use registry information.
- Use urlmon.dll's FindMimeFromData.
- Use IIS information.
- Roll your own MIME mapping function. Based on this table, for example.
I've been using no.1 for some time but I realized that the information provided by the registry is not consistent and depends on the software installed on the machine. Some extensions, like .zip don't use to have a Content-Type specified.
Solution no.2 forces me to have the file on disk in order to read the first bytes, which is something slow but may get good results.
The third method is based on Directory Services and all that stuff, which is something I don't like much because I have to add COM references and I'm not sure it's consistent between IIS6 and IIS7. Also, I don't know the performance of this method.
Finally, I didn't want to use my own table but at the end seems the best option if I want a decent performance and consistency of the results between platforms (even Mono).
Do you think there's a better option than using my own table or one of other described methods are better? What's your experience?
It depends what you need the MIME type for. In general, for services (web apps, web service, etc.), it's advisable not to use a MIME list which is dependent on the OS settings, or only as fallback if you cannot find MIME information otherwise.
I think that this is also the reason why MS chose to put constant MIME types in their
System.Web.MimeMapping
class (unfortunately it's internal, for whatever reason).Edit:
Wrapper (<= .NET 3.5)
Wrapper (.NET 4.0)
.NET 4.5+
No wrapper required, call the public method
System.Web.MimeMapping.GetMimeMapping
directly.I also found that the Init() method would be called and the _extensions and _mime members would not be completely initialized so I changed it to read, and the best way is, remove call GetExtension in constructor from MimeTypeCollection:
I have combined all these approaches in my utility lib, except maybe no.3. Btw, no.2 (urlmon.dll) doesn't require static file, it simply takes some bytes no matter where they had come from. Here's my current class
}
Nisus - would you be willing to post the entire source code for your utility somewhere? that would really be helpful. thanks!
Never mind....
I edited the apache definition file to only contain entries with defined extensions, then extended the code to load in the types/extensions from the text file at run time. Not elegant perhaps but sure beats creating/maintaining 630 lines of source code for the mime types.
[in the constructor for MimeTypeCollection instead of this stuff: this.Add(new mimeTypeInfo("application/applixware",new List(new[] { ".aw" })));]
I also found that the Init() method would be called and the _extensions and _mime members would not be completely initialized so I changed it to read:
Anyway, I now how a class that can handle the external defs and local registry I needed.
Thanks!
The
System.Web.MimeMapping
has 3 versions - two version 4.0s (where one of these is internal), and then a version 2.0 which is also internal. As pointed out, there is a public version of the class in System.Web version 4.0 for the .NET 4.5 framework.For RoadkillWiki I've basically reverse engineered it to save the bother of reflecting each time, the Wiki's file handler tries to the use IIS/applicationhost.config by default, and then falls through to the MimeMapping class:
And MimeMapping:
I've written a program to fetch and convert the Apache mime.types file to a C#
Dictionary<string, string>
keyed by file extension. It's here:https://github.com/cymen/ApacheMimeTypesToDotNet
The actual output is this file:
https://github.com/cymen/ApacheMimeTypesToDotNet/blob/master/ApacheMimeTypes.cs
Hopefully someone else finds it useful too!