3

I would like to redirect to an action controller gracefully after session expires. Added Session_OnEnd method in my Global.asax file. See code below:

Global.asax:

public void Session_OnEnd()
{
    //redirect to controller action here
}

At first I had "Response.RedirectToRoute" inside the method but throws an exception and can't work technically.

felixgondwe
  • 123
  • 1
  • 3
  • 11
  • 1
    When the session ends, that's just an observation that the user hasn't sent any requests to the server recently. You don't have an outstanding request from the user's browser, so you don't have an opportunity to send *anything* to it to cause it to do anything. HTTP is request/response, the server cannot *initiate* an action with the client. – Damien_The_Unbeliever Sep 17 '14 at 14:33
  • @Damien_The_Unbeliever Web Sockets to the rescue!!!!!!! – Matías Fidemraizer Sep 17 '14 at 14:44

1 Answers1

5

You can't do this. Session_End could be fired without an actual HTTP context. The user might have even closed his browser long before this event gets fired so there is nowhere to redirect to. The Request and Response objects are not available.

But you can create custom ActionFilter for handling this issue.

Redirect at Session Timeout in Global.asax in mvc4

Detecting Session expiry on ASP.NET MVC

Detecting Session Timeouts using a ASP.Net MVC Action Filter

Community
  • 1
  • 1
Mohsen Esmailpour
  • 11,224
  • 3
  • 45
  • 66