如何在 ASP.NET 模块中处理应用程序启动事件

发布于 2024-09-06 16:55:36 字数 276 浏览 1 评论 0原文

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

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

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

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.

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

何必那么矫情 2024-09-13 16:55:36

我会选择一个简单的属性,像这样......

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

这样你就可以通过属性从 Config 访问你需要的任何东西......

int someValue = Config.SomeValue;

并且它会加载到应用程序对象中(如果它还没有加载)

如果你需要配置基于每个用户而不是全局,然后只需使用 Session["MyConfig"] 而不是 Application["MyConfig"]

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"]

离旧人 2024-09-13 16:55:36

不确定这是否有效,但您也许可以在模块的 初始化方法

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

凉宸 2024-09-13 16:55:36

在 httpmodule 的 init 方法中,您可以连接到上下文中的事件。

例如 :

public void Init(HttpApplication context)
    {

        context.PostRequestHandlerExecute += (sender, e) =>
        {
            Page p = context.Context.Handler as Page;
            if (p != null)
            {
            ///Code here    
            }
        };
    }

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    
            }
        };
    }
安人多梦 2024-09-13 16:55:36
public SomeHttpModule : IHttpModule
{    
    private static readonly Configuration Configuration = 
            ConigurationReader.Read();    
}
public SomeHttpModule : IHttpModule
{    
    private static readonly Configuration Configuration = 
            ConigurationReader.Read();    
}
一抹淡然 2024-09-13 16:55:36

静态变量就达到了目的。如果有人感兴趣的话,这是代码 -

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


        }

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


        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文