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

  • Home
  • SEARCH
  • 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 8176463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:11:19+00:00 2026-06-06T23:11:19+00:00

So I have a Session variable which is set during the first user login

  • 0

So I have a Session variable which is set during the first user login

Session["ClientID"]

Basically this is used for theming (so the ClientID sets the theme/brand to appear on a website). Looking at the code applying

(Guid)Session["ClientID"] 

All over the place just seems dirty and error prone, what the best design pattern to use to get the variable globally recognized. So I can do

this.CurrentClientID

or something similar on all MVC Pages. In theory I could overload the Controller class with a Custom class providing this ID, but I’m not sure how I would expose this to the View as well.

Any pointers to the best solution would be gratefully received!

  • 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-06T23:11:20+00:00Added an answer on June 6, 2026 at 11:11 pm

    No idea what you mean globally, but an extension method to the ControllerBase class would render it accessible in all your controllers:

    public static class ControllerExtensions
    {
        public static Guid GetCurrentClientID(this ControllerBase controller)
        {
            return (Guid)controller.ControllerContext.HttpContext.Session["ClientID"];
        }
    }
    

    and now inside each controller you can access it:

    public ActionResult Foo()
    {
        Guid id = this.GetCurrentClientID();
        ...
    }
    

    And if you want it to be even more globally available make it an extension method to the HttpContextBase class:

    public static class ControllerExtensions
    {
        public static Guid GetCurrentClientID(this HttpContextBase context)
        {
            return (Guid)context.Session["ClientID"];
        }
    }
    

    now everywhere you have access to the HttpContext (which is pretty much everywhere in an ASP.NET application) you simply use the extension method. For example inside a view:

    @Html.ActionLink("foo link", "foo", new { clientid = Context.GetCurrentClientID() })
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this session variable which i am trying to set to an access
I have a session variable that I set like this: <?php $token = md5(uniqid(rand(),
I have a web-based system built with user login data based on session variables.
I have one PHP script with a session variable, set like so: $_SESSION['VAR1'] =
I have in many places in my ASP.NET project used the Session variable for
I have a certain session variable that is actually an array. I want to
I am currently in C# and I have set Session variables on each page.
within an asp.net webform project I have a session variable that I am populating
Ok, I've got this singleton-like web class which uses session to maintain state. I
I'm confused as to how to accomplish this. I have a page which, has

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.