2012-04-15 32 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(); 
     } 
    } 
}