简体   繁体   English

从页面访问HttpModule的属性

[英]getting access to property of HttpModule from page

Is there a way to get access to a property of HttpModule from asp.net page? 有没有办法从asp.net页面访问HttpModule的属性?

namespace MyHttpModule
{
    public class Module : IHttpModule
    {
        public string M_Property { get; set; }

        public void Init(HttpApplication context)
        {

You can get active modules from ApplicationInstance, for example I have module that saves current RawUrl in BeginRequest : 您可以从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;
    }
  }

Of course you have to register your module in web.config : 当然你必须在web.config中注册你的模块:

  <system.web>
    <httpModules>
      <add name="PlainModule" type="WebApplication1.PlainModule, WebApplication1"/>
    </httpModules>
  </system.web>

and then you can get module instance using name registered in web config, like this : 然后你可以使用在web配置中注册的名称来获取模块实例,如下所示:

protected void Page_Load(object sender, EventArgs e)
{
  PlainModule pm = (PlainModule)HttpContext.Current.ApplicationInstance.Modules["PlainModule"];
  Response.Write("Current request URL : "  + pm.CurrentRequestUrl);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM