Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I query IIS for MIME Type Mappings?

How can I programatically read IIS's MIME types? I'd like to use them when I stream data to my clients using WCF.

Any tips, or API would be appreciated

like image 1000
makerofthings7 Avatar asked Oct 14 '10 22:10

makerofthings7


People also ask

Where is MIME types IIS?

In IIS Manager, right-click the local computer, and click Properties. Click the MIME Types button.

How do I find MIME type files?

For detecting MIME-types, use the aptly named "mimetype" command. It has a number of options for formatting the output, it even has an option for backward compatibility to "file". But most of all, it accepts input not only as file, but also via stdin/pipe, so you can avoid temporary files when processing streams.

Which MIME type is default MIME type present in IIS?

If IIS does not recognize the file name extension requested by the client, IIS sends the content as the default MIME type, which is Application.


1 Answers

I'm making the assumption this is IIS7 only and you're using C#3.0 or later:

using Microsoft.Web.Administration;
....
using(ServerManager serverManager = new ServerManager())
{
  // If interested in global mimeMap:
  var config = serverManager.GetApplicationHostConfiguration();

  // Use if interested in just a particular site's mimeMap:
  // var config = serverManager.GetWebConfiguration("Default Web Site");

  var staticContent = config.GetSection("system.webServer/staticContent");
  var mimeMap = staticContent.GetCollection();

  // Print all mime types
  foreach (var mimeType in mimeMap)
  {
    Console.WriteLine(String.Format("{0} = {1}", mimeType["fileExtension"],
         mimeType["mimeType"]));
  }

  // Find a mime type based on file extension
  var mt = mimeMap.Where(
        a => (string) a.Attributes["fileExtension"].Value == ".pdf"
      ).FirstOrDefault();

  if (mt != null)
  {
    Console.WriteLine("Mime type for .pdf is: " + mt["mimeType"]);
  }
}

You need to reference the Microsoft.Web.Administration.dll in c:\windows\system32\inetsrv.

Your code also needs Administrator rights to be able to do this as well.

like image 64
Kev Avatar answered Sep 24 '22 07:09

Kev