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

查看:136
本文介绍了如何查询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"]);
  }
}

您需要参考 Microsoft.Web.Administration.dll c:\ windows \ system32 \inetsrv

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

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

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

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