How to handle application start event in ASP.NET module

心已入冬 提交于 2019-12-05 20:29:54

I'd go for a simple property, something like this ...

public MyConfig Config
{
    get
    {
        MyConfig _config = Application["MyConfig"] as MyConfig;
        if (_config == null)
        {
            _config = new MyConfig(...);
            Application["MyConfig"] = _config;
        }
        return _config;
    }
}

that way you just access whatever you need from Config via the property ...

int someValue = Config.SomeValue;

and it's loaded into the application object if it hasn't been already

If you need the config on a per-user basis rather than globally, then just use Session["MyConfig"] instead of Application["MyConfig"]

Not sure if this would work, but you might be able to implement this in the module's init method.

In the init method of your httpmodule you can hook up to the event in the context.

For example :

public void Init(HttpApplication context)
    {

        context.PostRequestHandlerExecute += (sender, e) =>
        {
            Page p = context.Context.Handler as Page;
            if (p != null)
            {
            ///Code here    
            }
        };
    }
public SomeHttpModule : IHttpModule
{    
    private static readonly Configuration Configuration = 
            ConigurationReader.Read();    
}

static variable did the trick. here is the code if someone is interested -

static string test; 
        public void Init(HttpApplication application)
        {


            application.BeginRequest +=(new EventHandler(this.Application_BeginRequest));
            test = "hi"; 
            application.EndRequest +=(new EventHandler(this.Application_EndRequest));


        }
       private void Application_BeginRequest(Object source,EventArgs e)
        {
            {
                HttpApplication application = (HttpApplication)source ;
                HttpContext context = application.Context;
                context.Response.Write(test);
            }


        }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!