如何在 IIS 中查询 MIME 类型映射? [英] How can I query IIS for MIME Type Mappings?

查看:33
本文介绍了如何在 IIS 中查询 MIME 类型映射?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何以编程方式读取 IIS 的 MIME 类型?当我将数据流式传输到我的客户时,我想使用它们 使用 WCF.

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

任何提示或 API 将不胜感激

Any tips, or API would be appreciated

推荐答案

我假设这只是 IIS7,而您使用的是 C#3.0 或更高版本:

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"]);
  }
}

您需要在c:windowssystem32inetsrv中引用Microsoft.Web.Administration.dll.

您的代码也需要管理员权限才能执行此操作.

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

这篇关于如何在 IIS 中查询 MIME 类型映射?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆