我想将 MIME 类型添加到新创建的虚拟目录(位于“默认网站”下)。
using (ServerManager manager = new ServerManager())
{
ServerManager manager = new ServerManager();
var vdir = manager.Sites["Default Web Site"].Applications["/"].VirtualDirectories["VDir"];
}
我还没有找到合适的示例/文档,可以在不使用 DirectoryServices
的情况下为虚拟目录(而不是网站)执行此操作。有什么建议吗?
最佳答案
你试过这样吗(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();
}
}
}
关于c# - 将 Mime 类型添加到虚拟目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10162237/