Реализация настроенного метода application_end в asp.net

Я хочу иметь метод, который выполняется, когда истекает сеанс, или пользователь выходит из системы, или пользователь закрывает веб-приложение. Как я могу поймать эти события в asp.net и выполнить метод? Я создаю веб-приложение в версии 2008/asp.net/ C#.

Пожалуйста, помогите мне.

Спасибо в ожидании

2 ответа

Решение

Щелкните правой кнопкой мыши на решении и добавьте новый элемент, добавьте Global.asax в решение, после чего выполните следующее событие.

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

Событие Session_start(),Session_End() и Application_End() вы сможете отслеживать событие.

use Global.asax file 

Перейдите по ссылке, чтобы использовать файл Global.asax http://www.dotnetcurry.com/ShowArticle.aspx?ID=126

Другие вопросы по тегам