Add a MIME Type to a Virtual Directory IIS C#

已答复 Add a MIME Type to a Virtual Directory IIS C#

  • 2012年4月14日 9:20
     
      包含代码
    I am trying to add a MIME type to a virtual directory in IIS programmatically. Previously, in my code, I have used Microsoft.Web.Administration classes to create web application, virtual directory and perform some other operations.

    However, I could only find examples to add a MIME type using another DirectoryServices classes. I have just started working to configure IIS programmatically for a small requirement, so I am just familiar with the basics.

    The virtual directory SampleVDir is created under the Default Web Site and to this Virtual Directory, I have to add a MIME type.

        using (ServerManager manager = new ServerManager())
                    {
                        var vdir = manager.Sites["Default Web Site"].Applications["/"].VirtualDirectories["SampleVDir"];
                        var config = manager.GetWebConfiguration("Default Web Site");
                        var staticContent = config.GetSection("system.webServer/staticContent");
                        var MimeMap = staticContent.GetCollection();
                        MimeMap.Add(//??//);
                    }
    

    I am not sure about some things:

    1. Instead of getting the configuration of a website, I have to get the configuration for a virtual directory. Should I add the MIME type in the virtual directory or to the Default Web Site itself and will my app still work if the MIME type is in website and not in VDir

    2. The staticContent variable fetches from some section system.webServer/staticContent. Although it works for me to fetch all the mime types for the default web site, what exactly is this staticContent?

    3. To add a MimeType to the above MimeMap var, I have to add a configuration element for the mime type. MimeType.Add(`Config element`). I am not sure what exactly is to be added here for fileExtension and fileType of my MIME Type.

全部回复

  • 2012年4月17日 3:15
    版主
     
     

    Hi optimus,

    Welcome!

    I think you should repost your question here: http://forums.iis.net/1034.aspx for a better support, thanks for understanding.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

  • 2012年4月17日 4:13
     
     已答复

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

    Regards,

    Narendran Ponpandiyan