HttpModulesSection Management?

X

xeroxero

I have multiple HTTPModules defined in my ASP.NET 2.0 web site, and
want to remove/add one via a web page button click. I know that
making a change will cause the web app to restart, which is
what I want.

I have obtained the collection of HttpModules with this code:

HttpModulesSection httpModules =
config.GetSection("system.web/httpModules")
as HttpModulesSection;

But I do not understand how to toggle one to be used or not.
For example, here is how one is defined in the web.config:

<add type="TestFilter,TestAssembly2" name="TestFilter" />

How can I toggle TestFilter to be enabled/disabled?


Thanks.
 
S

Steven Cheng[MSFT]

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>&nbsp; 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();
}
}
 
X

xeroxero

Thanks for taking the time to do all of that. My understanding is that
HttpModules are accessed in a certain order, and just adding/removing
will place a module that was normally 3rd in the order at the bottom,
which would mess up the request-flow.

How can the add be modified to place a given module at the nth
position?

Thanks.


Hello Xeroxero,
[snip]
 
S

Steven Cheng[MSFT]

Hello Xeroxero,

Thanks for the reply.

Yes, I think this a good question:). Based on my research, the
HttpModulesSection class doesn't provide methods to directly add a new
module at a specific postion. I think this is reasonable because the
HttpModulesSection.Modules collection contains modules not only defined in
our application's web.config file, but also in super level web.config (or
root machine level web.config). Therefore, it will be hard to do such
insertAt (before or after) through the methods of the
HttpModulesSection.Modules collection.

Currently, I think it would be more flexible to directly operate on the
RawXml of the HttpModuleSection through its "SectionInformation" property.
You can get the HttpModules Section's RawXML and set new XML for it
directly. e.g.

#the GetRawXml method will only return the XML fragment configured in our
application web.config file(does not include any setting in super level)
==========
protected void btnSetRaw_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;

Response.Write("<br/>Raw XML: " +
Server.HtmlEncode(modulesection.SectionInformation.GetRawXml()));


}

config.Save();
}
=============================

You can directly assign raw XML by using a raw string value:

=====================
protected void btnSetRaw_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;



modulesection.SectionInformation.SetRawXml("<httpModules><clear/></httpModul
es>");


}

config.Save();
}
========================

Or you can load it into an XMLDocument and manipulate it though DOM api and
write it into HttpModulesSection later:

#the following code add two new modules one by one into web.config's
httpmodules section through SetRawXml
=====================
protected void btnSetRaw_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;


XmlDocument doc = new XmlDocument();
doc.LoadXml(modulesection.SectionInformation.GetRawXml());

XmlElement simpleModule =
doc.SelectSingleNode("/httpModules/add[@name='simpleModule']") as
XmlElement;
XmlElement testModule =
doc.SelectSingleNode("/httpModules/add[@name='testModule']") as XmlElement;

if (simpleModule != null && testModule != null)
{
//do nothing;
}
else if(simpleModule != null)
{
testModule = doc.CreateElement("add");
testModule.SetAttribute("name", "testModule");
testModule.SetAttribute("type", "TestRewriteModule");

doc.DocumentElement.InsertAfter(testModule, simpleModule);
}
else
{
simpleModule = doc.CreateElement("add");
simpleModule.SetAttribute("name", "simpleModule");
simpleModule.SetAttribute("type","SimpleRewriteModule");

testModule = doc.CreateElement("add");
testModule.SetAttribute("name", "testModule");
testModule.SetAttribute("type", "TestRewriteModule");

doc.DocumentElement.AppendChild(simpleModule);
doc.DocumentElement.InsertAfter(testModule, simpleModule);
}

modulesection.SectionInformation.SetRawXml(doc.OuterXml);

}



config.Save();
}
============================


Hope this helps.


Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



This posting is provided "AS IS" with no warranties, and confers no rights.
 
S

Steven Cheng[MSFT]

Hi Xeroxero,

Have you got any further progress on this or does my last reply helps a
little?

Please feel free to let me know if there is anything else we can help.


Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top