Search code examples
asp.net-mvc-4sessionrazorresponse.redirect

Response.Redirect doesn't run using Razor in View


@{
    if (Session["LoginType"] == null)
    {
       Response.Redirect("~/Account/Login", true);
    }

}

If the Session expires, the Session["LoginType"] becomes null. I can catch that. But the next line to redirect to the login page again, doesn't work. Yellow code line in break mode just stops on it, then moves on to next lines of code (that gives an error because the Session["LoginType"] is null.

Any ideas? Am I coding this wrong? Not really an expert at this MVC/Razor stuff.


Solution

  • You should be performing this type of logic in the controller rather than the view.