5

The code I've used to get the HTTP Modules is basically

HttpModulesSection modules = ((SystemWebSectionGroup)config.GetSectionGroup("system.web")).HttpModules;
// Depending on what we need to do...
//modules.Modules.Add(CreateSomeModule());
//modules.Modules.Remove("SomeOtherModule");

This worked fine up until IIS7. The migration command %SystemRoot%\system32\inetsrv\appcmd migrate config "website/" moves the modules into system.webServer, so my code is now updating the wrong section.

Is there a built in way to get the proper module section that should be modified? Or do I have to add a check for the Request.ServerVariables["SERVER_SOFTWARE"] and return system.web/system.webServer depending on the string I get back?

Community
  • 1
  • 1
Brandon
  • 68,708
  • 30
  • 194
  • 223
  • 1
    Please clarify what you are doing with the list of modules. – John Saunders Jan 20 '11 at 04:04
  • @John, we create a new HttpModuleAction and then we just add it to the HttpModules collection. Updated the question to show a clearer example. – Brandon Jan 20 '11 at 16:42
  • why are you editing the web.config? Also, what code are you doing this from? The same web site that owns the web.config? – John Saunders Jan 20 '11 at 17:33
  • @John, Isn't that what the `modules.Modules.Add` does? It adds the necessary line into the web.config file to reference the module. The code is on the website that owns the web.config. It is not my website, so I'm not 100% sure on how the modules work, except that they have to be referenced in the web.config to work. – Brandon Jan 20 '11 at 17:53
  • no, one does not usually modify web.config. As user53870 told you below, you only need to modify the Modules property – John Saunders Jan 20 '11 at 18:10

1 Answers1

1
HttpContext.Current.ApplicationInstance.Modules

this returns a HttpModuleCollection object. Or do you want to know how to programmatically register HttpModules at runtime?

Community
  • 1
  • 1
Elian Ebbing
  • 18,779
  • 5
  • 48
  • 56
  • Thanks for the answer, however that returns an HttpModuleCollection. I need the HttpModulesSection so that I can add/remove a module in the code. I don't believe I need to register the mdoules, I just need to edit the web.config. – Brandon Jan 20 '11 at 16:45