添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
有腹肌的大象  ·  Routing — Sanic-cn ...·  4 天前    · 
刚毅的花卷  ·  android recyclerView ...·  5 天前    · 
朝气蓬勃的油条  ·  boost/asio/detail/reso ...·  1 周前    · 
无邪的灌汤包  ·  WildFly 24·  1 周前    · 
风流的自行车  ·  TreeFrog Framework | ...·  1 周前    · 
暴躁的电梯  ·  Holtwinters ...·  4 月前    · 

What is it all about?

This post shows you how to create a keep alive for a .NET website so people can stay logged in whilst the page is open, instead of it timing out after 20 minutes.

The basic premise to this is:

  • Create a handler which responds with an OK message and doesn't allow caching.
  • Add some javascript to the page to call the handler every 10 minutes.
  • The call to the server side handler keeps the session alive.

Here's how to do it:

  1. Create a folder in your web project called Handlers , if you don't have one already.
  2. Add a class called KeepAlive.cs
  3. Add the following code to the KeepAlive class
  4. namespace CodeShare.Web.Handlers public class KeepAlive : IHttpHandler, IRequiresSessionState public void ProcessRequest(HttpContext context) context.Response.AddHeader("Cache-Control", "no-cache"); context.Response.AddHeader("Pragma", "no-cache"); context.Response.ContentType = "text/plain"; context.Response.Write("OK"); public bool IsReusable { get { return false; } } <handlers> <remove name="KeepAlive" /> <add name="KeepAlive" verb="GET" path="<mark>keep-alive.ashx</mark>" type="CodeShare.Web.Handlers.KeepAlive " /> </handlers> </system.webServer> </configuration>
    //calls the keep alive handler every 600,000 miliseconds, which is 10 minutes
    var keepAlive = {
        refresh: function () {
            $.get('<mark>/keep-alive.ashx</mark>');
            setTimeout(keepAlive.refresh, 6000000);
    }; $(document).ready(keepAlive.refresh());