Get custom HTTPModule parameters in web.config?

I am creating an HTTPModule that can be reused several times, but with different parameters. As an example, consider the query redirection module. I could use HTTPHandler, but this is not a task for him, because my process should work at the request level, and not at the extension / path level.

Anyway, I would like to have my web.config as follows:

<system.webServer> <modules> <add name="tpl01" type="TemplateModule" arg1="~/" arg2="500" /> <add name="tpl02" type="TemplateModule" arg1="~/" arg2="100" /> </modules> </system.webServer> 

But most of the information I could find was this . I say: yes, I can get the whole <modules> , but how does each instance of my HTTPModule know which arguments to take? If I could get the name ( tpl01 or tpl02 ) after creation, I could later look at its arguments by name, but I did not see any property in the HTTPModule class to get this.

Any help would really be appreciated. Thank you in advance!:)

+5
source share
2 answers

This may be a workaround for your problem.

First, define your module with fields for what you need to install from the outside:

 public class TemplateModule : IHttpModule { protected static string _arg1; protected static string _arg2; public void Init(HttpApplication context) { _arg1 = "~/"; _arg2 = "0"; context.BeginRequest += new EventHandler(ContextBeginRequest); } // ... } 

Then from your web application, every time you need to use a module with a different set of these values, inherit the module and redefine the fields:

 public class TemplateModule01 : Your.NS.TemplateModule { protected override void ContextBeginRequest(object sender, EventArgs e) { _arg1 = "~/something"; _arg2 = "500"; base.ContextBeginRequest(sender, e); } } public class TemplateModule02 : Your.NS.TemplateModule { protected override void ContextBeginRequest(object sender, EventArgs e) { _arg1 = "~/otherthing"; _arg2 = "100"; base.ContextBeginRequest(sender, e); } } 
-1
source

I think that this part of config (system.webServer \ modules \ add) is not intended to transfer (store) parameters to modules, but to register a list of modules to process a request.

For possible atttributes in the add element, see - https://msdn.microsoft.com/en-us/library/ms690693(v=vs.90).aspx

+2
source

All Articles