c# - 将 Mime 类型添加到虚拟目录

标签 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 的情况下为虚拟目录(而不是网站)执行此操作。有什么建议吗?

最佳答案

你试过这样吗(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/

相关文章:

c# - FileSystemWatcher 删除事件处理程序

iis - Log Parser 2.2 跳过今天的 IIS 日志

javascript - Express:在 304 Not Modified 上发送 Content-Type

java - 无法将大于 1.5 MB 的文件添加到存储库

c# - 流行的开源库和引用冲突

c# - 如何找出当前的溢出检查上下文?

asp.net - azure在云服务中重新运行启动任务而不重新启动角色

c# - 如何正确使用 IRegisteredObject 阻止应用程序域关闭/回收 Web 应用程序?

javascript - XHTML 模式下 MathOverflow 的宝贵天赋

c# - 取消后不再显示unity Game Center登录对话框