3

Is adding / removing HttpModules and HttpHandlers at runtime from a website a good idea ? I am looking at ways to make my website as dynamic as possible without uploading changed content all over again. Something like config files. Rule engines exist, but learning them seems like a pain. I might be wrong in my perspective. Please suggest possible ways to change logic in the code at runtime.

pokrate
  • 3,954
  • 7
  • 30
  • 36

3 Answers3

7

ASP.NET 4 provides a PreApplicationStartMethod capability that can be used to add HttpModules programmatically.

I just did a blog post on that at http://www.nikhilk.net/Config-Free-HttpModule-Registration.aspx.

The basic idea is you create a derived HttpApplication that provides ability to add HttpModules dynamically at startup time, and it then initializes them into the pipeline whenever each HttpApplication instance is created within the app-domain.

Nikhil Kothari
  • 5,215
  • 2
  • 22
  • 28
5

For .NET4 and above:

For dynamically installing a HttpHandler, you need to create a HttpModule wrapper to do that. See the answer to: Any way to add HttpHandler programmatically in .NET?

To dynamically add a http module:

Install-Package Microsoft.Web.Infrastructure

Install-Package WebActivator

[assembly: PreApplicationStartMethod(typeof(MyPreStartClass), "Start")]

namespace .....
{
    public static class MyPreStartClass
    {
        public static void Start()
        {
            DynamicModuleUtility.RegisterModule(typeof(FooHttpModule));
        }
    }
}
Community
  • 1
  • 1
Chris Marisic
  • 32,487
  • 24
  • 164
  • 258
1

One possible way is create a generic HTTPHandlerFactory which in turn returns the requested handlers.

Check out the link for more clarity

RameshVel
  • 64,778
  • 30
  • 169
  • 213