Custom Search
Logiclabz
  • Home
  • C#
  • Programmatically Adding IIS Application Extension Mapping using .Net C#

Programmatically Adding IIS Application Extension Mapping using .Net C#

  

Application Extension Mapping can be programmatically added using "DirectoryEntry" class in "System.DirectoryServices" namespace.

using System.DirectoryServices;


	public class DirEntry
        {

	public void SetSingleProperty(string metabasePath, string propertyName, object newValue)
            {
                //  metabasePath is of the form "IIS://servername/path"
                try
                {
                    DirectoryEntry path = new DirectoryEntry(metabasePath);
                    PropertyValueCollection propValues = path.Properties[propertyName];
                    string oldValue = propValues.Value;
                    path.Properties[propertyName][0] = newValue;
                    path.CommitChanges();
                    Console.WriteLine("Done");
                }
                catch (Exception ex)
                {
                    if ("HRESULT 0x80005006" == ex.Message)
                        Console.WriteLine(" Property {0} does not exist at {1}", 
propertyName, metabasePath);
                    else
                        Console.WriteLine("Failed in SetSingleProperty with the 
following exception: \n{0}", ex.Message);
                }
            }

	}
"SetSingleProperty" method can be used to add any DirectoryEntry. For adding IIS Application Extension Mapping "ScriptMaps" Property to be added. i.e
DirEntry de = new DirEntry();
de.SetSingleProperty("IIS://localhost/W3SVC/1/ROOT", "ScriptMaps", 
@".htm,c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,5,GET, HEAD, POST");



  


Leave a reply


Comments

  • Kevin says:
    Jun 24, 11

    Hi, This would work, but don't run this, this will remove ALL other extensions and keep only this one you are creating... adsutil.vbs is the best way to go, google David Wang. He hacked this file which allows you to add a (one) extension with out affecting any other extension... Trust me i have done this, pulled my hair out and just got it working! Know your infrastructure.

  • Mike says:
    Feb 03, 12

    Should be able to do this though: path.Properties["ScriptMaps"].Add(@".htm,c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,5,GET, HEAD, POST")



Do you like this post?