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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:18:40+00:00 2026-05-31T13:18:40+00:00

I am not sure I am asking the right question here. I have a

  • 0

I am not sure I am asking the right question here.

I have a shared page (master page) that calls a couple of partial pages for side menu, header, footer etc.. and all my controllers inherit a BaseController.

Now, depending on the user login status, I need to show different data in all those partial pages and I thought where is the best place to check whether a user is logged in or not – BaseController.

And therein lies my problem. I need to contact one of my web services to see if a user is logged in and get some relevant data if he is. I only need to do this once, and since all controllers inherit from BaseController, each of those partial page calls results in the web service call.

Obviously, I cannot just stick a private bool variable isUserAuthenticated and check for flag, as, each controller will have a new instance of the base controller.

In traditional asp.net projects, I would put this stuff in HttpContext.Current.Items[] and use re-use it but I cannot (somehow) access that in MVC.

I cannot just not inherit from basepage on partial pages as they can also be called independently and I need to know the user login status then too.

What is the best way to call a function just once, or, rather, store a bool value for the duration of one call only? – accessible between controlers..

How do people do this?
thanks, sorry, I’m a newbie to mvc!

  • 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-31T13:18:41+00:00Added an answer on May 31, 2026 at 1:18 pm

    You can still use HttpContext.Items, but you’ll need to access it via a HttpContextBase instance.

    For backwards compatibility you can wrap an HttpContext in an HttpContextWrapper, like so

    var context = new HttpContextWrapper(HttpContext.Current);
    

    @iamserious’s answer above suggests using a static property – which I strongly disagree with. Setting a static variable is application wide and would mean each and every user would be using the same variable – so all would have the same login data. You want to store it either per user in Session or per Request via HttpContext.Items.


    I’d suggest doing something using like this approach, then no matter where you call ContextStash.GetInstance, you’ll receive the same instance for the lifetime of the same request. You could also follow the same pattern and use HttpContext.Session instead of HttpContext.Items:

    // could use this.HttpContext inside a controller, 
    // or this.Context inside a view, 
    // or simply HttpContext.Current
    var stash = ContextStash.GetInstance(this.HttpContext);
    
    if(!stash.IsSomething)
    {
        // do something to populate stash.IsSomething
    }
    
    // class
    public class ContextStash
    {
        const string cacheKey = "ContextStash";
    
        public ContextStash(HttpContextBase context)
        {
            // do something with context
        }
    
        // your shared properties
        public bool IsSomething { get; set; }
        public string Foo { get; set; }
        public int Bar { get; set; }
    
        // instance methods
        public static ContextStash GetInstance()
        {
            return GetInstance(new HttpContextWrapper(HttpContext.Current));
        }
    
        public static ContextStash GetInstance(HttpContext context)
        {
            return GetInstance(new HttpContextWrapper( context ));
        }
    
        public static ContextStash GetInstance(HttpContextBase context)
        {
            ContextStash instance = context.Items[cacheKey] as ContextStash;
            if(null == instance)
            {
                context.Items[cacheKey] = instance = new ContextStash(context);
            }
            return instance;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am not sure that I am asking right question. But I have a
I'm not sure I'm asking the right question here, but I'm looking to provide
Not even sure if I'm asking the right question, but here it goes. Basically
I'm not sure that's the right plattform asking this question. I'm searching a solution
Not sure I'm asking the right question, but it's a start. I have a
I've googled around but i'm not sure i am asking the right question or
I am not sure if I am asking the right question. But is it
Not sure if I'm asking the right question so please bear with me! Bit
Not sure if this is the right stack to be asking this question, but
I am not sure if i am asking the right question or not. I

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.