I am trying to do a simple redirect to login page if session expires in asp.net mvc 4.5. I am trying it as follows.
Global.asax
protected void Session_OnEnd(object sender, EventArgs e)
{
Response.RedirectToRoute("Default"); //default is route
}
but here null exception comes and response object is not found. I have seen this post but could not figure out as it is pretty broad.
UPDATE
I have tried applying filter as follows now it does not crash but also does not redirect to error page.
SessionTimeoutAttribute
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
HttpContext ctx = HttpContext.Current;
if (HttpContext.Current.Session["SchoolCode"] == null)
{
filterContext.Result = new RedirectResult("~/Views/Shared/Error");
return;
}
base.OnActionExecuting(filterContext);
}
Added Attribute to Controller class
[SessionTimeout]
public class MapsController : Controller{}
Why doesnt it redirect?