Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
599 views
in Technique[技术] by (71.8m points)

iis 7 - 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:windowssystem32inetsrv.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...