Usman ur Rehman Ahmed's blog

How to manipulate web.config file programatically?

DNN provides robust mehcnaism for taking care of configuration chagnes in web.config file by means of config type componetns in the manifest file but they can be dangerous as a slight mistake can bring down the whole web site and second, it doesn't provide option switches to make entries based over conditions. Such conditional entry is required just as the case with IIS7 integrated mode.

Up untill IIS6, the section <httpModules> was enough for an http module to be executed rightly but with the launch of IIS7, an entry of http module is required within <modules> section as well if IIS7 with running integrated mode.

The method shared below takes care of integrated mode and can be used if such configuration change is required based over user interaction. That is, the design of module or provider be updated in a way that user interaction is required to enable/disable an entry.

        public static bool AddHttpModuleEntry(string name, string type)
        {
            try
            {
                XmlDocument document = new XmlDocument();
                document.Load(HttpContext.Current.Server.MapPath("~/web.config"));
                bool nodeFound = false;
                XmlNodeList nodes;
                bool toSave = false;

                //For IIS6 or IIS7 (normal mode)
                nodes = document.SelectNodes("/configuration/system.web/httpModules/add");
                if (nodes != null)
                {
                    //Look for the desired node
                    foreach (XmlNode node in nodes)
                    {
                        if (node.Attributes["name"].Value == name)
                        {
                            nodeFound = true;
                        }
                    }
                    if (!nodeFound)
                    {
                        //Required node doesn't exist, add it
                        XmlNode node = document.CreateElement("add");
                        XmlAttribute nameAttribute = document.CreateAttribute("name");
                        nameAttribute.Value = name;
                        XmlAttribute typeAttribute = document.CreateAttribute("type");
                        typeAttribute.Value = type;
                        node.Attributes.Append(nameAttribute);
                        node.Attributes.Append(typeAttribute);
                        nodes[0].ParentNode.AppendChild(node);
                        toSave = true;
                    }
                }


                //For IIS7 (integrated mode)
                nodeFound = false;
                if (HttpRuntime.UsingIntegratedPipeline)
                {
                    //Add an entry in the system.webServer section (this is specific to IIS7 integrated mode)
                    nodes = document.SelectNodes("/configuration/system.webServer/modules/add");
                    if (nodes != null)
                    {
                        //Look for the desired node
                        foreach (XmlNode node in nodes)
                        {
                            if (node.Attributes["name"].Value == name)
                            {
                                nodeFound = true;
                            }
                        }
                        if (!nodeFound)
                        {
                            //Required node doesn't exist, add it
                            XmlNode node = document.CreateElement("add");
                            XmlAttribute nameAttribute = document.CreateAttribute("name");
                            XmlAttribute typeAttribute = document.CreateAttribute("type");
                            XmlAttribute preConditionAttribute = document.CreateAttribute("preCondition");

                            nameAttribute.Value = name;
                            typeAttribute.Value = type;
                            preConditionAttribute.Value = "managedHandler";

                            node.Attributes.Append(nameAttribute);
                            node.Attributes.Append(typeAttribute);
                            node.Attributes.Append(preConditionAttribute);
                            nodes[0].ParentNode.AppendChild(node);

                            toSave = true;
                        }
                    }
                }

                if (toSave)
                {
                    document.Save(HttpContext.Current.Server.MapPath("~/web.config"));
                }
                return true;
            }
            catch {
                throw;
            }
        }

Similar approach can be used to make changes (add/edit/delete) for any given section in any configuration (mostly web.config) file.

11
To Posterous, Love Metalab