Home
C#.ASP.NET
Application EndRequest and Response.End
Updated Dec 8, 2023
Dot Net Perls
EndRequest. All things have ends and beginnings. In ASP.NET we have the Global.asax file where we can handle requests to our website.
With Application_EndRequest, we can execute a method at the end of every request. We can call Response.End to force a request to end (we can do this in BeginRequest).
Example file. Consider this Global.asax file. We have a static List of strings—we add to this in the Application_EndRequest method.
And In Application_BeginRequest, write the ended requests to the current response with Response.Write.
Detail We call Response.End to terminate a Request in Application_BeginRequest. This causes EndRequest to execute.
<%@ Application Language="C#" %> <script runat="server"> static List<string> _endList = new List<string>(); void Application_BeginRequest(object sender, EventArgs e) { // Display the contents of our List. if (_endList.Count >= 1) { Response.Write("LAST END: " + _endList[_endList.Count - 1]); Response.Write(" END COUNT: " + _endList.Count); } // End this request. Response.End(); } void Application_EndRequest(object sender, EventArgs e) { // Log end messages. _endList.Add("END: " + DateTime.Now); } void Application_Start(object sender, EventArgs e) { // Code that runs on application startup } void Application_End(object sender, EventArgs e) { // Code that runs on application shutdown } void Application_Error(object sender, EventArgs e) { // Code that runs when an unhandled error occurs } void Session_Start(object sender, EventArgs e) { // Code that runs when a new session is started } void Session_End(object sender, EventArgs e) { // Code that runs when a session ends. // Note: The Session_End event is raised only when the sessionstate mode // is set to InProc in the Web.config file. If session mode is set to StateServer // or SQLServer, the event is not raised. } </script>
LAST END: END: 5/16/2017 2:04:32 PM END COUNT: 6
Notes, Global.asax. We can add a Global.asax class to our website in Visual Studio by going to "Add New Item" and then "Global Application Class."
Record ending requests. If we have some logging code that needs to run at the end of each request, then we have an ideal place to put it with Application_EndRequest.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Dec 8, 2023 (edit).
Home
Changes
© 2007-2025 Sam Allen