2012-04-15 74 views
1

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

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

我一直没能找到合适的例子/文档,在那里我可以做到这一点对一个虚拟目录(而不是一个网站),而无需使用DirectoryServices。有什么建议么?

回答

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(); 
     } 
    } 
}