将Mime Type添加到虚拟目录

时间:2012-04-15 13:00:11

标签: c# iis mime-types virtual-directory

我想将MIME类型添加到新创建的虚拟目录中,该目录位于“默认网站”下。

using (ServerManager manager = new ServerManager())
{
ServerManager manager = new ServerManager();
var vdir = manager.Sites["Default Web Site"].Applications["/"].VirtualDirectories["VDir"];
}

我没有找到适当的示例/文档,我可以在不使用DirectoryServices的情况下为虚拟目录(而不是网站)找到它。有什么建议吗?

1 个答案:

答案 0 :(得分:1)

您是否尝试过这样做(稍微修改the example on IIS.NET):

using System;
using System.Text;
using Microsoft.Web.Administration;

internal static class Sample
{
   private static void Main()
   {
      using (ServerManager serverManager = new ServerManager())
      {
         Configuration vDirConfig = serverManager.GetWebConfiguration("Default Web Site", "/VDir");
         ConfigurationSection staticContentSection = vDirConfig.GetSection("system.webServer/staticContent");
         ConfigurationElementCollection staticContentCollection = staticContentSection.GetCollection();

         ConfigurationElement mimeMapElement = staticContentCollection.CreateElement("mimeMap");
         mimeMapElement["fileExtension"] = @"bla";
         mimeMapElement["mimeType"] = @"application/blabla";
         staticContentCollection.Add(mimeMapElement);

         ConfigurationElement mimeMapElement1 = staticContentCollection.CreateElement("mimeMap");
         mimeMapElement1["fileExtension"] = @"tab";
         mimeMapElement1["mimeType"] = @"text/plain";
         staticContentCollection.Add(mimeMapElement1);

         serverManager.CommitChanges();
      }
   }
}