开发者

ASP.NET: variable that can be accessed by entire site

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 ac开发者_StackOverflowcess it's variables from page contents. Thanks in advance.


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


You could create a static class with a static member:

public static MyClass
{
  public static MyVariable { get; set; }
}

Then from anywhere in the site you can call MyClass.MyVariable to get or set the value.

Keep in mind a significant difference between this and PHP. In PHP, you're running scripts and you include other scripts to make one big script. In ASP.NET, you're compiling code into assemblies. The patterns are different.


Indeed, just to have something like global php variable static classes could help you. But is it A Really what you whant? Because global variables are known to be harder mantainable, testable, etc...

Probably you want to "put" data somewhere and "take" it some where else.

There are A LOT out of the box ways to do it in .NET.

You can use SessionState to store something while session with a user lasts
http://msdn.microsoft.com/en-us/library/ms178581.aspx

You can use ViewState as a short term...
http://msdn.microsoft.com/en-us/library/ms972976.aspx

You can use settings to setup variables in web.config
http://msdn.microsoft.com/en-us/library/b5ysx397.aspx

You can use Profiles to store something related to user
http://msdn.microsoft.com/en-us/library/2y3fs9xs.aspx

You can use Application to store something more globally
http://msdn.microsoft.com/en-us/library/ms178594.aspx

And there are more...
Treat this post as "links" addition to Karl Nicoll's post.


You can access a property ( or field or method) on the master page by the Master property on your content page, which gives you a reference to the master page. You will need to cast it to a type that supports the property first, though:

((Site)Master).MyVariable

And MyVariable has to be visible to the content page, public or internal I think. And ideally you'd cast to a base type for the Master page, not directly as in the example above.


If it is a config option, use web.config file app settings section

If it is a session variable there is a session object

And you can also create an static class with some public members.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜