Hello Xeroxero,
As for the HttpModulesSection you get from the ASP.NET application's
Configuration object(from WebConfigurationManager), its "Modules"
collection contains the registered httpModules applied on this
application(include both the httpmodules defined in appliation's web.config
file or in parent web application or root web.config or machine level
web.config ...). You can use the following code to display them all:
===============
protected void btnDisplay_Click(object sender, EventArgs e)
{
Configuration config =
WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
ConfigurationSection section =
config.GetSectionGroup("system.web").Sections["httpModules"];
if (section != null)
{
HttpModulesSection modulesection = section as
HttpModulesSection;
foreach (HttpModuleAction module in modulesection.Modules)
{
Response.Write("<br/>name: " + module.Name + ", type: " +
module.Type);
}
}
}
===================
If you want to disable an existing httpmodule defined in super level
web.config or in our application's own web.config, you can simply remove it
from the HttpModuleSection setting and save the configuration(you only need
to supply the module's name). e.g.
==================
protected void btnRemove_Click(object sender, EventArgs e)
{
Configuration config =
WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
ConfigurationSection section =
config.GetSectionGroup("system.web").Sections["httpModules"];
if (section != null)
{
HttpModulesSection modulesection = section as
HttpModulesSection;
if (!string.IsNullOrEmpty(txtRemove.Text))
{
modulesection.Modules.Remove(txtRemove.Text);
}
}
config.Save();
}
=========================
If you want to add a new httpmodule which hasn't been registered yet(in
application web.config or super level web.config), you can create a new
HttpModuleAction class instance and add it into HttpModuleSection.Modules
collection (you need to supply the name and type attributes). e.g.
=======================
protected void btnAdd_Click(object sender, EventArgs e)
{
Configuration config =
WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
ConfigurationSection section =
config.GetSectionGroup("system.web").Sections["httpModules"];
if (section != null)
{
HttpModulesSection modulesection = section as
HttpModulesSection;
HttpModuleAction newmodule = new
HttpModuleAction(txtAddName.Text, txtAddType.Text);
modulesection.Modules.Add(newmodule);
}
config.Save();
}
========================
Your change in code which be reflected in the web.config's <httpModules>
section. If you've removed/disabled a module defined in super level
web.config, you'll find a new <remove> element in the <httpModules>
section. If you're removing a module originally defined in this
application's own web.config, you won't find such a <remove> element, but
find that the original <add> element disappear.
I've pasted the complete test page's aspx and codebehind at the bottom for
your reference. If you have anything unclear or any further questions on
this, please feel free to let me know.
Sincerely,
Steven Cheng
Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no rights.
======= aspx template================
<html xmlns="
http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
Add Name:
<asp:TextBox ID="txtAddName"
runat="server">rewriteModule</asp:TextBox> Add Type:
<asp:TextBox ID="txtAddType"
runat="server">SimpleRewriteModule</asp:TextBox><br />
Remove Name:
<asp:TextBox ID="txtRemove" runat="server"></asp:TextBox><br />
<br />
<asp:Button ID="btnDisplay" runat="server" Text="Display"
OnClick="btnDisplay_Click" />
<asp:Button ID="btnAdd" runat="server" Text="Add"
OnClick="btnAdd_Click" />
<asp:Button ID="btnRemove" runat="server" Text="Remove"
OnClick="btnRemove_Click" />
</div>
</form>
</body>
</html>
========code behind=====================
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Configuration;
public partial class admin_AdminConfigPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnDisplay_Click(object sender, EventArgs e)
{
Configuration config =
WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
ConfigurationSection section =
config.GetSectionGroup("system.web").Sections["httpModules"];
if (section != null)
{
HttpModulesSection modulesection = section as
HttpModulesSection;
foreach (HttpModuleAction module in modulesection.Modules)
{
Response.Write("<br/>name: " + module.Name + ", type: " +
module.Type);
}
}
}
protected void btnAdd_Click(object sender, EventArgs e)
{
Configuration config =
WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
ConfigurationSection section =
config.GetSectionGroup("system.web").Sections["httpModules"];
if (section != null)
{
HttpModulesSection modulesection = section as
HttpModulesSection;
HttpModuleAction newmodule = new
HttpModuleAction(txtAddName.Text, txtAddType.Text);
modulesection.Modules.Add(newmodule);
}
config.Save();
}
protected void btnRemove_Click(object sender, EventArgs e)
{
Configuration config =
WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
ConfigurationSection section =
config.GetSectionGroup("system.web").Sections["httpModules"];
if (section != null)
{
HttpModulesSection modulesection = section as
HttpModulesSection;
if (!string.IsNullOrEmpty(txtRemove.Text))
{
modulesection.Modules.Remove(txtRemove.Text);
}
}
config.Save();
}
}