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 7562639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:27:32+00:00 2026-05-30T13:27:32+00:00

Question: I want to implement a sessionAccess class, which throws a SessionExpired-Exception when an

  • 0

Question:

I want to implement a sessionAccess class, which throws a “SessionExpired”-Exception when an attempt to access an expired session is made.
And I want to show a special page for SessionExpired, instead of the YSOD.

This is what I have:
In Global.asax.cs

MvcApplication : System.Web.HttpApplication
{

        // http://stackoverflow.com/questions/183316/asp-net-mvc-handleerror
        public class SessionExpiredErrorHandlerAttribute : HandleErrorAttribute
        {

            public override void OnException(ExceptionContext exceptionContext)
            {
                //Logger.Error(exceptionContext.Exception.Message,exceptionContext.Exception);
                //exceptionContext.ExceptionHandled = true;

                // http://blog.dantup.com/2009/04/aspnet-mvc-handleerror-attribute-custom.html
                UrlHelper url = new UrlHelper(HttpContext.Current.Request.RequestContext);
                string messagePageUrl = url.Action("SessionExpired", "Home").ToString();
                System.Web.HttpContext.Current.Response.Redirect(messagePageUrl, true);

                base.OnException(exceptionContext);
            } // End Sub OnException

        } // End Class MyErrorHandlerAttribute


        // http://freshbrewedcode.com/jonathancreamer/2011/11/29/global-handleerrorattribute-in-asp-net-mvc3/
        // <customErrors mode="On" />
        // <customErrors mode="RemoteOnlyy" />
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {

            filters.Add(new HandleErrorAttribute());

            filters.Add(new SessionExpiredErrorHandlerAttribute
            {
                ExceptionType = typeof(WebApplications.SessionAccess.SessionExpiredException),
                View = "@btw WHY is anything I write here ignored ???, and why TF can one only set the view, and not the controller as well @",
                Order = 2

            });


        } // End Sub RegisterGlobalFilters


}

And this is my SessionAccess class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;


namespace WebApplications
{


    // http://stackoverflow.com/questions/2950064/detect-when-a-users-session-has-exipred
    public class SessionAccess
    {

        // LoginFailedException
        public class SessionExpiredException : System.Exception
        {
            // The default constructor needs to be defined
            // explicitly now since it would be gone otherwise.

            public SessionExpiredException()
            {
            }

            public SessionExpiredException(string strKey)
                : base("Session \"" + strKey + "\" expired, or was never set.")
            {
            }

        }


        static System.Web.SessionState.HttpSessionState Session
        {
            get
            {
                if (System.Web.HttpContext.Current == null)
                    throw new ApplicationException("No Http Context, No Session to Get!");

                return System.Web.HttpContext.Current.Session;
            }
        }


        public static T Get<T>(string key)
        {
            System.Nullable<bool> SessionExists = (System.Nullable<bool>) Session["__sys_" + key + "_hasBeenSet"];
            if (SessionExists == null)
                throw new SessionExpiredException(key);

            if (Session[key] == null)
                return default(T);
            else
                return (T)Session[key];
        }


        public static void Set<T>(string key, T value)
        {
            Session["__sys_" + key + "_hasBeenSet"] = true;
            Session[key] = value;
        }


    } // End Class SessionAccess


} // End Namespace WebApplications 

And then, in the homecontroller, I implement this view:

  public ActionResult TestPage(string id)
        {


 /*
        WebApplications.SessionAccess.Set<string>("foo", "test");

        string str = WebApplications.SessionAccess.Get<string>("foo");
        Console.WriteLine(str);

        Session.Clear();
        Session.Abandon();

        str = WebApplications.SessionAccess.Get<string>("foo");
        Console.WriteLine(str);
        */

            throw new Exception("bogus");

            return View();
        }

Then I have a SessionExpired.cshtml, which I put into Views\Shared

Now, despite custom-error off, I can get the SessionExpired error message.
It works fine for SessionExpiredException, but the problem now is, that I get this exception for ANY exception (null reference, applicationexception, etc…)

Can anybody tell my why this is ?
I would have assumed I only ever get to this page on a SessionExpiredException…

Why on every other exception as well ????

For some reason, the inner workings of filter seems to be malfunctioning…

  • 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-05-30T13:27:34+00:00Added an answer on May 30, 2026 at 1:27 pm

    Default implementation of OnException checks HandleErrorAttribute.ExceptionType Property and return s if there’s no match. Code from HandleErrorAttribute.OnException :

            if (!ExceptionType.IsInstanceOfType(exception)) {
                return;
            }
    

    As you override OnException, you should add this check to your implementation too.

    You can download and inspect whole source of asp.net-mvc 3 in details if you need to.

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

Sidebar

Related Questions

I want to implement a scheduler class, which any object can use to schedule
i have question for example i want to implement binary tree with array i
I want to implement the following combobox in ExtJS. The question is, how to
I have a question similar to This one . But I want to implement
I want to implement list like stackoverflow question list (where each row has multiple
everyone I got a question , I want to implement a UI , Like
I think the question is quite self-explanatory. I want to implement a simple zoom
I want to implement a Omegle like chat system on a website, which will
(Related to a previous unanswered question I asked). I want to implement a function
For a toy project, I want to implement an automated question answering system with

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.