简体   繁体   English

关闭浏览器时调用代码隐藏方法

[英]Invoke code-behind method when browser is closing

I need to find a way to intercept browser closing and invoke a metod to update a record with several information about logged user in DB. 我需要找到一种方法来拦截浏览器关闭并调用一个方法以使用有关DB中已登录用户的若干信息来更新记录。 It's very important this record is updated when the user is logging-out or when he close the browser. 在用户注销或关闭浏览器时更新此记录非常重要。 Obviously when the user clicks 'Logout' I handle the update in the server-side event, but what if the user simply exit from browser? 显然,当用户单击“注销”时,我会在服务器端事件中处理更新,但是如果用户只是从浏览器中退出怎么办?

Someone suggest to use the window.onbeforeunload event and make an asynchronous call to some WS or WebMethod to execute the code, but this doesn't convince me at all: the problem with onbeforeunload is that it shows a confirm prompt. 有人建议使用window.onbeforeunload事件并异步调用某些WS或WebMethod来执行代码,但这一点根本无法说服我: onbeforeunload的问题在于它显示了一个确认提示。 I need to avoid this message and simply invoke the method. 我需要避免出现此消息,而只需调用该方法。

So I'm wondering if there is a 'server-side' solution without using ajax or javascript. 所以我想知道是否有一个不使用ajax或javascript的“服务器端”解决方案。 For example... a way to trigger some event on session abandon or session clear, or some other way to solve this problem just working on code-behind... 例如...一种在会话放弃或会话清除时触发某些事件的方法,或仅在后台代码上工作即可解决此问题的其他方法...

您不可能有一个服务器端解决方案来了解客户端浏览器中发生的事情。

I do not believe there is any way to do what you need server-side only. 我不相信有任何方法可以只在服务器端执行所需的操作。 Client side is the only way. 客户端是唯一的方法。 Server has no way of knowing when browser window was closed, this is limitation of HTTP protocol. 服务器无法知道何时关闭浏览器窗口,这是HTTP协议的限制。

Yes, you can put an event in the Global.AsaX which will fire when the session ends. 是的,您可以在Global.AsaX中放置一个事件,该事件将在会话结束时触发。 Now if you need data from the client to update the db etc., you'll need a way of getting it there, but if not, then the Session_End will do the trick. 现在,如果您需要来自客户端的数据来更新数据库等,则需要一种将其保存到数据库的方法,但是如果没有,那么Session_End将可以解决问题。

Note: Session end is slightly different than the browser closing, so it this will depend on what you want the event firing to do. 注意:会话结束与浏览器关闭略有不同,因此这取决于您要事件触发的操作。

How to handle session end in global.asax? 如何在global.asax中处理会话结束?

I'd like to find a 'server-side' solution without using ajax or javascript. 我想找到一个不使用ajax或javascript的“服务器端”解决方案。

I suspect that it's impossible with that requirement. 我怀疑满足该要求是不可能的。

Maybe you could do something like: 也许您可以执行以下操作:

  1. Have a hidden IFRAME on the page 在页面上隐藏IFRAME
  2. Set the Refresh header on this IFRAME (or use a META element) to contact the server every couple of seconds 设置此IFRAME上的Refresh标头(或使用META元素)每隔几秒钟与服务器联系一次
  3. If you do not hear from the client for some period of time, assume the browser has been closed. 如果您一段时间未收到客户端的消息,请假定浏览器已关闭。

However, I imagine that this solution will not scale well. 但是,我认为此解决方案无法很好地扩展。

Have you considered something like signalr? 您是否考虑过信号器? I use it to detect when someone has a record open. 我用它来检测某人何时打开记录。

public class ChatHub : Hub
{
     public override Task OnDisconnected()
     {
         Broadcaster.Disconnected(Context.ConnectionId);
         return base.OnDisconnected();
     }
}

For the moment I changed radically the approach to my problem. 目前,我从根本上改变了解决问题的方法。 To update pending rows I implemented a timed job using Quartz.NET framework, that runs every night. 为了更新待处理的行,我使用Quartz.NET框架实施了定时作业,该作业每天晚上运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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