• Twitter
  • Facebook
  • Google+
  • Instagram
  • Youtube

Wednesday, August 22, 2007

Dynamically change web.config file

Dynamically change web.config file
==============================

Description

// 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

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


Code

// 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");

sec2.Modules.Remove("DJUpload");

objConfig.Save();

// Your web.cofig is now changed dynamically.

0 comments:

Contact

Get in touch with me


Adress/Street

12 Street West Victoria 1234 Australia

Phone number

+(12) 3456 789

Website

www.johnsmith.com