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.
Posted
by Usman ur Rehman Ahmed
About Me
Usman ur Rehman Ahmedis known as a software engineer in Lahore, Pakistan. He is renowned for having an abstract understanding of vast range of technological developments such as programming languages, web development, RIA's and documental writing, and is mainly specialized in developmental analysis.
Tags
- windows phone 7 (15)
- DNN (14)
- Dot Net Nuke (13)
- .NET (5)
- c# (5)
- manifest (4)
- Application (3)
- MySql (3)
- Provider (3)
- Sql (3)
- View all 237 tags »
- Sql Server (3)
- Stored Procedure (3)
- visual sutdio (3)
- API (2)
- Data (2)
- Mango (2)
- Module (2)
- Permission (2)
- PhoneTextBlock (2)
- User Control (2)
- Windows (2)
- config (2)
- console (2)
- developer (2)
- dot net (2)
- emulator (2)
- enumeration (2)
- expression blend (2)
- extension (2)
- loop (2)
- network (2)
- phone 7 (2)
- tools (2)
- transparent (2)
- .ascx (1)
- 3g (1)
- 5.6.2 (1)
- 7.1 (1)
- 7.5 (1)
- AJAX (1)
- Activity (1)
- App.xaml (1)
- AppManifest (1)
- ApplicationIcon (1)
- Arabic (1)
- Background.png (1)
- BuildAction (1)
- Connector (1)
- Control Panel (1)
- Core Version (1)
- Data-tier (1)
- DataBound (1)
- Database (1)
- Dependency (1)
- DeviceNetworkInterface (1)
- Directory (1)
- Dynamic (1)
- EVDO (1)
- Encryption (1)
- File (1)
- FileSystemWatcher (1)
- Filter (1)
- Foreign Key (1)
- Function (1)
- GUID (1)
- GetSiteLog (1)
- Hash (1)
- Host Settings (1)
- IIS6 (1)
- IIS7 (1)
- Integrated Mode (1)
- Internet Explorer (1)
- IsolatedStorage (1)
- Java (1)
- Koder (1)
- Linq (1)
- Lists (1)
- Log (1)
- Marketplace (1)
- Membership (1)
- Microsoft (1)
- NavigationCacheMode (1)
- NavigationContext (1)
- NavigationService (1)
- Notify (1)
- OnClientNodeChecked (1)
- PTCL (1)
- Panaroma (1)
- Password (1)
- Properties (1)
- Query String (1)
- ResXResourceReader (1)
- ResolveHostNameAsync (1)
- Right to Left (1)
- SDK (1)
- SEO (1)
- SendKeys (1)
- SendWait (1)
- SharpPCap (1)
- Stream (1)