简体   繁体   中英

implementation of customized application_end method in asp.net

I want to have a method which gets executed when session expires or user logs out or user closes the web application. How can i catch these events in asp.net and execute a method ? I'm building a web app in vs 2008/asp.net/c#.

Please help me.

Thanks in anticipation

use Global.asax file 

请参阅链接以使用Global.asax文件http://www.dotnetcurry.com/ShowArticle.aspx?ID=126

Right click on The solution and the Add new item the Add Global.asax in the solution Then after which have the following Event

 <script runat="server">



        void Application_Start(object sender, EventArgs e)
        {
            // Code that runs on application startup
            //Utils.LoadExtensions();

        }

        void Application_End(object sender, EventArgs e)
        {
            ClsCollege ObjClsColledge = new ClsCollege();
            ObjClsColledge.TruncateAllUserDetails(Session["UserSessionId"].ToString());
            ObjClsColledge.TruncateAllUserDetailsPrefrance(Session["UserSessionId"].ToString());

        }

        void Application_Error(object sender, EventArgs e)
        {

            HttpContext context = ((HttpApplication)sender).Context;
            Exception ex = context.Server.GetLastError();
            if (ex == null || !(ex is HttpException) || (ex as HttpException).GetHttpCode() == 404)
            {
                return;
            }
            StringBuilder sb = new StringBuilder();

            try
            {
                sb.AppendLine("Url : " + context.Request.Url);
                sb.AppendLine("Raw Url : " + context.Request.RawUrl);

               while (ex != null)
                {
                    sb.AppendLine("Message : " + ex.Message);
                   sb.AppendLine("Source : " + ex.Source);
                   sb.AppendLine("StackTrace : " + ex.StackTrace);
                   sb.AppendLine("TargetSite : " + ex.TargetSite);
                   ex = ex.InnerException;
                }
            }
            catch (Exception ex2)
            {
                sb.AppendLine("Error logging error : " + ex2.Message);
            }

            if (BlogSettings.Instance.EnableErrorLogging)
            {
               Utils.Log(sb.ToString());
           }
            context.Items["LastErrorDetails"] = sb.ToString();
            context.Response.StatusCode = 500;

            //// Custom errors section defined in the Web.config, will rewrite (not redirect)
            //// this 500 error request to error.aspx.


        }

        void Session_Start(object sender, EventArgs e)
        {




         }

        void Session_End(object sender, EventArgs e)
        {
            ClsCollege ObjClsColledge = new ClsCollege();
            ObjClsColledge.TruncateAllUserDetails(Session["UserSessionId"].ToString());
            ObjClsColledge.TruncateAllUserDetailsPrefrance(Session["UserSessionId"].ToString());
        }






</script>

The Event Session_start(),Session_End() and Application_End() you will able to track the Event.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM