Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8908995
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:19:31+00:00 2026-06-15T03:19:31+00:00

I am working on a website which is API based, client side is being

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T03:19:32+00:00Added an answer on June 15, 2026 at 3:19 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a google map api key which is working in my website but
I'm currently working to specify my company's new partner/public API, which will be a
I am working on a website which needs to work across multiple browsers unfortunately
I am working on a website which needs to be responsive. I want to
We are working on a website which is small but it use Flash Navigation,
I'm working on a website which has advert banners which link to external pages.
i am working on a website which deploy on web farms to serve high
I'm currently working on a website which is displaying users tweets from the twitter
I am currently working on a website which lists the businesses as per either
I am working on a website on which i show restaurants according to either

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.