Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET HttpApplication.EndRequest event not fired

Tags:

.net

asp.net

According this MSDN article HttpApplication.EndRequest can be used to close or dispose of resources. However this event is not fired/called in my application.

We are attaching the handler in Page_Load the following way:

HttpContext.Current.ApplicationInstance.EndRequest += ApplicationInstance_EndRequest;

The only way is to use the Application_EndRequest handler in Global.asax, but this is not acceptable for us.

like image 421
Crank Avatar asked Oct 27 '08 13:10

Crank


2 Answers

You can use your own HttpModule to capture the EndRequest if you don't want to use the global.asax.

public class CustomModule : IHttpModule 
{
    public void Init(HttpApplication context)
    {
        context.EndRequest += new EventHandler(context_EndRequest);
    }

    private void context_EndRequest(object sender, EventArgs e)
    {
        HttpContext context = ((HttpApplication)sender).Context;
        // use your contect here
    }
}

You need to add the module to your web.config

<httpModules>
    <add name="CustomModule" type="CustomModule"/>
</httpModules>
like image 84
Eduardo Campañó Avatar answered Sep 28 '22 06:09

Eduardo Campañó


Per the MSDN documentation, this event occurs AFTER the page is completed, just like BeginRequest. Therefore as far as I know it is not possible to catch this at the page level

like image 23
Mitchel Sellers Avatar answered Sep 28 '22 05:09

Mitchel Sellers