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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With