Monday, August 10, 2009

Dynamically change web.config file

Dynamically change web.config file


// Suppose you want to remove a handler from 'httpHandlers'
// and you want to remove modules from your 'httpModules'
// section of you web.config file..

// Below, this is suppose your web.confile file's sections

<add verb="GET" type="DJ.Blog.FileUpload.UploadProgressHandler, FileUploadLibrary" path="DJUploadProgress.ashx" />
<add name="DJUpload" type="DJ.Blog.FileUpload.UploadModule, FileUploadLibrary"/>


// Now, you want to remove both section from you web.config file.

// Using these Namespaces

using System.Configuration;
using System.Web.Configuration;

// Write this code to your page_load() event

Configuration objConfig = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
HttpHandlersSection sec = (HttpHandlersSection) objConfig.GetSection("system.web/httpHandlers");
sec.Handlers.Remove("GET", "DJUploadProgress.ashx");

HttpModulesSection sec2 = (HttpModulesSection)objConfig.GetSection("system.web/httpModules");



// Your web.cofig is now changed dynamically.

No comments: