有没有办法从 asp.net 页面访问 HttpModule 的属性?
namespace MyHttpModule
{
public class Module : IHttpModule
{
public string M_Property { get; set; }
public void Init(HttpApplication context)
{
最佳答案
您可以从ApplicationInstance获取事件模块,例如我有一个在BeginRequest中保存当前RawUrl的模块:
public class PlainModule : IHttpModule
{
private HttpApplication app = null;
public string CurrentRequestUrl;
public void Init(HttpApplication Context)
{
this.app = Context;
Context.BeginRequest += new System.EventHandler(Begin);
}
public void Dispose()
{
}
private void Begin(Object Sender, EventArgs e)
{
this.CurrentRequestUrl = this.app.Request.RawUrl;
}
}
当然你必须在 web.config 中注册你的模块:
<system.web>
<httpModules>
<add name="PlainModule" type="WebApplication1.PlainModule, WebApplication1"/>
</httpModules>
</system.web>
然后你可以使用在网络配置中注册的名称获取模块实例,如下所示:
protected void Page_Load(object sender, EventArgs e)
{
PlainModule pm = (PlainModule)HttpContext.Current.ApplicationInstance.Modules["PlainModule"];
Response.Write("Current request URL : " + pm.CurrentRequestUrl);
}
关于asp.net - 从页面访问 HttpModule 的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11949341/