如何处理ASP.NET模块中的应用程序启动事件 [英] How to handle application start event in ASP.NET module

查看:55
本文介绍了如何处理ASP.NET模块中的应用程序启动事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个asp.net HTTP模块,该模块需要从本地文件(例如存储在应用程序根目录中的config.xml)一次读取配置数据,然后根据配置对传入的请求.

I am writing an asp.net HTTP module which needs to read configuration data once from a local file (say config.xml stored in application root directory) and then based on configuration perform some processing on incoming requests.

由于Asp.NET模块中没有可用的Application_Start/Application_init挂钩,因此处理该方案的最佳方法是什么.我试图避免每次请求到来时都读取配置文件.理想情况下,我想在应用程序启动时读取配置文件.

Since there is no Application_Start/Application_init hooking available in Asp.NET modules, what would be the best way to handle the scenario. I am trying to avoid reading configuration file each time a request comes. Ideally, I want to read the config file when application starts.

我只需要在http模块中编写此代码,并且不想使用Global.asax

I need to code this in http module only and do not want to use Global.asax

推荐答案

静态变量可以解决问题.如果有人感兴趣,这是代码-

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);
            }


        }

这篇关于如何处理ASP.NET模块中的应用程序启动事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆