1

I am working on a website which is API based, client side is being developed in .Net MVC. For exception handling, I am using

 public void Application_Error(object sender, EventArgs e)
        {
            string action = "Index";
            Exception exception = Server.GetLastError();
            Response.Clear();

            HttpException httpException = exception as HttpException;

            if (httpException != null)
            {


                switch (httpException.GetHttpCode())
                {
                    case 404:
                        // page not found
                        action = "Error404";
                        break;
                    default:
                        action = "Index";
                        break;
                }

                // clear error on server
                Server.ClearError();
            }
            Response.Redirect(String.Format("/error/{0}", action));
        }

so for any exception thrown by try catch from Controller, the page redirects to error page.

Now I want that when session is expired it should redirect to Login page, How can I do that?

Right now what is happening is, after session expires, when I try to access the session value, it throws exception "object reference not set to an instance of object." then it redirects to the default error page.

1 Answer 1

3

I don't think you're going to be able to do this from inside a generic exception handler because - as you said - missing session variables simply throw a NullReferenceException. Perform a null check on the session variable from your controller:

Public ActionResult MyAction ()
{
    if (Session["myVariable"] == null)
    {
        RedirectToAction("SessionTimeOut", "Error");
    }

    ...

}

If you have session variables that should always exist unless the session has expired, you could try overriding the OnActionExecuting method for your controller and performing your null check in there. To do this for multiple controllers, define a BaseController, override its OnActionExecuting method and then inherit this in your other controllers.

Sign up to request clarification or add additional context in comments.

4 Comments

I have already tried using this but in my case, the controllers are accessible with login and without login as well. When user is logged in he sees a menu with some additional items, and in this case when session expires and he clicks on item which is accessible after login, the nullreferenceexception is being thrown.
Try putting the [Authorize] attribute on the controller action that requires login - then handle the 401 error in your method like you did with the 404.
I am handling [Authorize] attribute from web.config. I have defined a controller in web.config on which it redirects id [Authorize] attribute is there and request is not authorized. Anyways, I am thinking to with your suggestion of overriding OnActionExecuting, in that case, will the Ajax request also be redirected if session expired, or for AJAX I have to use some separate method?
The Ajax request will be redirected - your browser won't. If you use the [Authorize] approach, you can handle the 401 response in your jQuery Ajax call as described here: stackoverflow.com/questions/6991749/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.