1

I am trying to redirect when in method Session_OnEnd() in Global.asax

If I type

public void Session_OnStart()
{
    HttpContext.Current.Response.Redirect("http://www.google.es");
}

And go to Google but when I try this onEnd...

public void Session_OnEnd()
{
    HttpContext.Current.Response.Redirect("http://www.google.es");
}

Shows

"NullReferenceException was unhandled by user code", Object reference not set to an instance of an object.

Anyone helps me with this please??

ssilas777
  • 9,672
  • 4
  • 45
  • 68
David91
  • 311
  • 2
  • 3
  • 8

1 Answers1

5

You have a misunderstanding of how Session_OnEnd() occurs. This happens when the session times out, which typically means the user has no active connection for a period of time. Since there is no active connection, there is no active request, therefor you cannot redirect anyone.

What I think you're trying to do is redirect the users browser when the session times out, and there is no way to do that from the server. You have to do this in JavaScript on the client after a timeout period of inactivity.

Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291
  • Yes Erik, I am trying just this, but, what is the form to make this? Any example? Thanks – David91 Feb 11 '15 at 15:53
  • May be good idea to mention that for persistent session state providers the Session_OnEnd does not fire at all... – Alexei Levenkov Feb 11 '15 at 15:56
  • @David91 - I would suggest asking a different question, as that one is JavaScript based and has nothing to do with MVC or server side. – Erik Funkenbusch Feb 11 '15 at 15:57
  • @David91- that would be different duplicate :) Basically `setTimeout(20*60*1000, function(){location = "logout.aspx"})` is a reasonable starting point. – Alexei Levenkov Feb 11 '15 at 15:59