如何在asp .net mvc应用程序中定期检查网络连接? [英] How to check network connection periodically in asp .net mvc application?

查看:65
本文介绍了如何在asp .net mvc应用程序中定期检查网络连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过网络访问我的Mvc Web应用程序,当网络由于某种原因断开连接并再次连接时,我能够访问我离开的页面。但是我希望在网络断开连接并再次连接而不是继续它离开的网页时实现这个场景,它将显示登录页面。

请帮我解决如何实现它

I am accessing my Mvc web application over a network, when network disconnected for some reason and again connected , i am able to access the page where i left. But i want to implement the scenario when network disconnected and again connected instead of continuing the webpage where it left , it will show the Login Page.
Please help me how to implement it

推荐答案

我不能说它是最好的/完美的想法,但你可以尝试一下 -



1.添加一个隐藏字段,用于保持intenet断开状态的状态。确保在第一次调用时将值设置为true,这意味着客户端仍然连接到互联网。

I can't claim it to be best/perfect idea but you can give it a try-

1. Add a hidden field to keep status of intenet disconnection status. Make sure that upon first call the value is set to "true" which means client is still connected to the internet.
<asp:hiddenfield id="hfInternetDisStatus" runat="Server" value="True" xmlns:asp="#unknown"></asp:hiddenfield>



2.由于您需要从客户端进行检查,所以Javacript是个不错的选择。我们需要在javascript中运行一个计时器,在计时器功能中我们需要编写逻辑来检查inetrnet是否断开连接。

用于Internet连接检查的功能


2. As you need to check this from client side, so Javacript is a good option. We need to run a timer in javascript and inside the timer funtion we need to write the logic for checking if inetrnet is disconnected.
Function For Internet Connectivity Check

<script type="text/javascript">
function CheckInternetConnection() {
  var status = navigator.onLine;
  var hfInternetDisStatus=document.getElementById("  <%=hfInternetDisStatus.ClientID%>");
  if (!status) {     
     hfInternetDisStatus.value="Flase"
  }
  //if you want to autoredirect to login page once internet is available
  //if(status && hfInternetDisStatus.value=="False")
  //{
       //window.location="Login.aspx";
  //}
}
</script>



计时器功能


Function for timer

<script type="text/javascript">
function StartMyTimer()
{
   setInterval(function () {CheckInternetConnection()}, 1000);
}
</script>





3. 设置计时器



3. Set the timer

<body onload="StartMyTimer();" />





在断开连接后互联网可用时,您可以将用户重定向到登录页面,如评论中所示代码或者如果您想要重定向用户,如果他/她与页面不相符,则重定向到登录页面应该根据隐藏字段中包含的值进入您的相应/常用方法。



我没有执行任何代码片段,这只是一个想法开始。你可能需要根据你的要求添加/修改代码。



希望,它有帮助:)



Either you can redirect the user to the login page once internet is available after disconnection which is illustrated in the commented code or if you want to redirect the user if he/she ineracts with the page then the redirect to login page should go in your respective/common method based upon the value contained in the hidden field.

I haven't executed any of the code snippets, this just an idea to start with. You may need to add/modify the code as per your requiremet.

Hope, it helps :)


互联网工作在网络连接之上的层,所以这在技术上不可能做到......不是没有客户端上与服务器保持永久连接的东西,这是一件坏事。互联网是专门设计的,它可以做到这一点......当连接断开时继续工作,所以你希望实现一些直接违反协议设计要实现的东西,所以你是挣扎。



这听起来像是人们梦寐以求的那些毫无意义的要求,而不是寻找解决方案,你应该问为什么这是一个要求首先。
The internet works at layers above the network connection, so this isn't going to be technically possible to do...not without something on the client that keeps a permanent connection to the server, and that's a bad thing. The internet was specifically designed so that it can do this very thing....go on working when a connection goes down, so you're looking to implement something that goes directly against what the protocols were designed to achieve, so you're going to struggle.

This sounds to me like one of those pointless requirements people dream up, and rather than looking for a solution you should instead ask why this is a requirement in the first place.


这篇关于如何在asp .net mvc应用程序中定期检查网络连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆