C# 获取HTTPModule';在web.config中有自己的参数吗?
我正在创建一个HTTPModule,它可以重复使用几次,但参数不同。以请求重定向器模块为例。我可以使用HTTPHandler,但它不是它的任务,因为我的流程需要在请求级别工作,而不是在扩展/路径级别工作 无论如何,我希望我的web.config是这样的:C# 获取HTTPModule';在web.config中有自己的参数吗?,c#,asp.net,web-config,arguments,httpmodule,C#,Asp.net,Web Config,Arguments,Httpmodule,我正在创建一个HTTPModule,它可以重复使用几次,但参数不同。以请求重定向器模块为例。我可以使用HTTPHandler,但它不是它的任务,因为我的流程需要在请求级别工作,而不是在扩展/路径级别工作 无论如何,我希望我的web.config是这样的: <system.webServer> <modules> <add name="tpl01" type="TemplateModule" arg1="~/" arg2="500" />
<system.webServer>
<modules>
<add name="tpl01" type="TemplateModule" arg1="~/" arg2="500" />
<add name="tpl02" type="TemplateModule" arg1="~/" arg2="100" />
</modules>
</system.webServer>
. 我说,是的,我可以获得整个
标记,但是我的HTTPModule的每个实例如何知道要接受哪些参数呢?如果我可以在创建时获得名称(tpl01
或tpl02
),那么我可以在创建后按名称查找它的参数,但是我在HTTPModule类中没有看到任何属性来获得该名称
任何帮助都是非常受欢迎的。提前感谢!:) 我认为,配置的这一部分(system.webServer\modules\add)不是要向模块传递(存储)参数,而是要注册模块列表以处理请求
有关“添加”元素中可能的属性,请参见-,这可能是您的问题的解决方法
首先,使用需要从外部设置的字段定义模块:
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);
}
// ...
}
然后,从web应用程序中,每当您需要使用具有不同值集的模块时,继承模块并覆盖字段:
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);
}
}
HttpModuleCollection具有AllKeys属性。对于每个密钥,您可以获取(key)并检查返回值是否等于模块的this,以确定模块的名称。这只是一个可怕的等待发生的竞争条件错误。在基类中创建一个新的虚拟方法,并使用不同于子类的参数调用该方法。不要使用静态字段来传递参数,这很愚蠢。