Application EndRequest and Response.End
This page was last reviewed on 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 tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Dec 8, 2023 (edit).
© 2007-2024 Sam Allen.