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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:42:46+00:00 2026-06-18T11:42:46+00:00

I have an action filter checks whether or not a session variable ID is

  • 0

I have an action filter checks whether or not a session variable ID is set. For development purposes, I have manually set this variable prior to the check.

public class MyActionFilterAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuted(ActionExecutedContext context)
        {

            context.HttpContext.Session.Add("ID", 123123);

            int ID = (int)context.HttpContext.Session.Contents["ID"];
            var rd = context.HttpContext.Request.RequestContext.RouteData;

            TED _db = new TED();

            //if not in DB
            if (_db.Users.Find(ID) == null && rd.GetRequiredString("action") != "NoAccess")
            {
                RouteValueDictionary redirectTargetDictionary = new RouteValueDictionary();
                redirectTargetDictionary.Add("action", "NoAccess");
                redirectTargetDictionary.Add("controller", "Home");
                redirectTargetDictionary.Add("area", "");

                context.Result = new RedirectToRouteResult(redirectTargetDictionary);
            }

            base.OnActionExecuted(context);
        }
    }

As far as I understand, this code is run prior to any page, this Session["ID"] is always set. The site works fine if I am consistently testing, but it seems to break if I leave it for a while then try to continue testing. Here is the error I get:

int UserID = (int)Session.Contents["ID"];
System.NullReferenceException: Object reference not set to an instance of an object.

Initially I thought the session may simply be expiring, but prior to any page loading, Session["ID"] should be getting set. What is the issue here?

  • 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-18T11:42:47+00:00Added an answer on June 18, 2026 at 11:42 am

    you’re implementing your actionfilter’s on OnActionExecuted method which executes AFTER your action method

    You should implement the OnActionExecuting method

    public class MyActionFilterAttribute : ActionFilterAttribute
        {
            public override void OnActionExecuting(ActionExecutingContext context)
            {
    
                context.HttpContext.Session.Add("ID", 123123);
    
                int ID = (int)context.HttpContext.Session.Contents["ID"];
                var rd = context.HttpContext.Request.RequestContext.RouteData;
    
                TED _db = new TED();
    
                //if not in DB
                if (_db.Users.Find(ID) == null && rd.GetRequiredString("action") != "NoAccess")
                {
                    RouteValueDictionary redirectTargetDictionary = new RouteValueDictionary();
                    redirectTargetDictionary.Add("action", "NoAccess");
                    redirectTargetDictionary.Add("controller", "Home");
                    redirectTargetDictionary.Add("area", "");
    
                    context.Result = new RedirectToRouteResult(redirectTargetDictionary);
                }
    
                base.OnActionExecuting(context);
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 2 action filter: [Auth] [Check] public ActionResult Home() { //... } I
I have a custom validation attribute derived from action filter attribute. Currently the attribute
I have controller users . In this controller I have action account which is
I have this action for updating data: def edit @project = Project.find(params[:id]) if @project.team
I have a global action filter that is setting the MasterPage of all ViewResults
So in my ClientsController.rb I have a before filter that does the following: Checks
I have a filter that queries the DB to check if the Username matches
I have a situation, I have a session bean with list, this list I
I have written a controller action that checks for the existence of a PDF
I have a filter setup that runs a function which checks if the user

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.