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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:51:46+00:00 2026-05-22T16:51:46+00:00

I am new to ASP .NET, I am trying to setup a website in

  • 0

I am new to ASP .NET, I am trying to setup a website in Visual Studio with C#.

My background is in PHP. In that language, if I want a variable to be accessible by every page, simply put it in a include file.

Is there anything similar to C# and ASP .NET? There is an site.master page, but I am not sure how to access it’s variables from page contents. Thanks in advance.

  • 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-22T16:51:47+00:00Added an answer on May 22, 2026 at 4:51 pm

    You have a few different options here:


    Session Variables
    Session variables are stored in the server’s memory for each user, and can be read and written to as often as required. These are limited to a per-user basis, so if you want to hold a single variable for all users, then this isn’t the way to go.

    Usage:

    Session["MyVariable"] = 5;
    int myVariable = (int)Session["MyVariable"]; //Don't forget to check for null references!
    

    You can to set a user’s session variable in your global.asax file under the session_start event handler if required.


    Application/Cache Variables
    Application and Cache variables are accessible by any user, and can be get/set as required. The only difference between the two is that Cache variables can expire, which makes them useful for things such as database query results, which can be held for a while before they’re out of date.
    Usage:

    Application["MyVariable"] = 5;
    int myVariable = (int)Application["MyVariable"]; //Don't forget to check for null references!
    

    You can set an application variable in your global.asax file in the application_start event handler if required.


    Web.Config
    This is probably the preferred way of storing constants in your application, since they are stored as “Application Settings” and changed in your web.config file as required without having to recompile your site. application settings are stored in the <appsettings> area of your file using this syntax:

    <appSettings>
      <add key="MyVariable" value="5" />
    </appSettings>
    

    Web.config values should be considered read-only in your code, and can simply be accessed using this code in your pages:

    int myVariable = (int)System.Configuration.ConfigurationSettings.AppSettings["MyVariable"];
    

    Static Variables
    Alternatively, you could just create a class that contains a static property to hold your variable like this:

    public class SiteVariables
    {
        private static _myVariable = 0;
        public static int MyVariable
        {
            get { return _myVariable; }
            set { _myVariable = value; }
        }
    }
    

    And then access it like this:

    int myVar = SiteVariables.MyVariable;
    

    I actually use a combination of the latter two solutions in my code. I’ll keep my settings in my web.config file, and then create a class called ApplicationSettings that reads the values from web.config when required using static properties.

    Hope this helps

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

Sidebar

Related Questions

I'm trying to start a new ASP.NET Web Application Project using Visual Studio 2008.
I have an asp.net application that i am modifying. I setup visual studio 2010
I'm trying to create a new ASP.NET website and add it to my subversion
I'm trying to setup Forms Authentication in an asp.net mvc 2 application that will
I am new to ASP.NET and am trying to convert a web application from
I have a new asp.net mvc project and i am trying to figure out
I'm trying to get the new ASP.NET Web API beta (VS 2010 default Web
I'm trying to use the new ASP.NET Web API to do the following: A
I'm trying to decide what validation approach to take for a new ASP.NET MVC
I am trying to start a new project (asp.net MVC) and I would like

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.