0

我想要一个在会话到期或用户注销或用户关闭 Web 应用程序时执行的方法。如何在 asp.net 中捕获这些事件并执行方法?我正在 vs 2008/asp.net/c# 中构建一个 Web 应用程序。

请帮我。

感谢期待

4

2 回答 2

2
use Global.asax file 

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

于 2011-04-16T09:49:27.750 回答
1

右键单击解决方案,然后在解决方案中添加新项目 Add 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() 您将能够跟踪事件。

于 2011-04-16T11:40:30.573 回答