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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:48:30+00:00 2026-06-18T08:48:30+00:00

I am working on a system that is segmented into yearly sessions. A user

  • 0

I am working on a system that is segmented into yearly sessions. A user can go and change the session to see past sessions

How would I go about passing the users current yearId to every controller?

I was thinking that I could set a users cookie on authentication or when they manually change their session and check the cookie using a global filter like so

public class MyTestAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        HttpCookie cookie = filterContext.HttpContext.Request.Cookies["myCookie"];

        //do something with cookie.Value
        if (cookie!=null) 
        {
           filterContext.ActionParameters["YearId"] = cookie.Value;
        }
        else
        {
           // do something here
        }
    }

    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        base.OnActionExecuted(filterContext);
    }
}

Here’s how I’d use the above filter: (or add it as a global filter)

[MyTestAttribute]
public ActionResult Index(int yearId)
{
    //Pass the yearId down the layers
    // _repo.GetData(yearId);
    return View();
}

With this approach, I would have to add yearId to every controller. Any feedback is appreciated.

  • 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-18T08:48:31+00:00Added an answer on June 18, 2026 at 8:48 am

    You could also create a base class for your Controllers that need the parameter as opposed to the filter:

    public class MyBaseController : Controller
    {
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            HttpCookie cookie = filterContext.HttpContext.Request.Cookies["myCookie"];
    
            //do something with cookie.Value
            if (cookie!=null) 
            {
               filterContext.ActionParameters["YearId"] = cookie.Value;
            }
            else
            {
               // do something here
            }
        }
    }
    

    Or you could even create a strongly-typed property and make it lazy, so that you don’t have to include it as a parameter to every action method and don’t perform the evaluation unless you access the property:

    public class MyBaseController : Controller
    {
        private int? _yearId;
    
        protected int YearId
        {
            get
            {
                 // Only evaluate the first time the property is called
                 if (!_yearId.HasValue)
                 {
                     // HttpContext is accessible directly off of Controller
                     HttpCookie cookie = HttpContext.Request.Cookies["myCookie"];
    
                     //do something with cookie.Value
                     if (cookie!=null) 
                     {
                          _yearId = int.Parse(cookie.Value);
                     }
                     else
                     {
                          // do something here
                     }
                 }
    
                 return _yearId.Value;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a system that relies in $_SERVER['REMOTE_ADDR'] to get the user address
I am working on a system that has been hashing user passwords with MD5
I have a working commenting system that is polymorphic so that comments can be
I am working on a system that splits users by organization. Each user belongs
I've got a working callback system that uses boost::signal. I'm extending it into a
I'm working on a system that needs to know a user's choice before they
I am working on a system that allows the user to generate objects from
I am working on a system that can create made up fanatsy words based
I am working on a system that processes documents that are dumped into a
I'm working on a system that generates a password for a user, place a

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.