【问题标题】:Add Mime Type to a virtual directory将 Mime 类型添加到虚拟目录
【发布时间】:2012-04-15 13:00:10
【问题描述】:

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

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

在不使用DirectoryServices 的情况下,我无法找到合适的示例/文档来为虚拟目录(而不是网站)执行此操作。有什么建议吗?

【问题讨论】:

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


    【解决方案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();
          }
       }
    }
    

    【讨论】:

      猜你喜欢
      • 2013-02-13
      • 2010-09-15
      • 2015-02-04
      • 2019-11-03
      • 2011-01-07
      • 2010-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多