ASP.NET:整个站点都可以访问的变量

发布于 2024-11-09 09:29:48 字数 191 浏览 0 评论 0原文

我是 ASP .NET 的新手,我正在尝试使用 C# 在 Visual Studio 中设置一个网站。

我的背景是 PHP。在该语言中,如果我希望每个页面都可以访问变量,只需将其放入包含文件中即可。

有没有类似于 C# 和 ASP .NET 的东西?有一个 site.master 页面,但我不确定如何从页面内容访问它的变量。提前致谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

や莫失莫忘 2024-11-16 09:29:48

这里有几个不同的选项:


会话变量
会话变量存储在每个用户的服务器内存中,并且可以根据需要随时读取和写入。这些仅限于每个用户,因此如果您想为所有用户保留单个变量,那么这不是可行的方法。

用法:

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

如果需要,您可以在 session_start 事件处理程序下的 global.asax 文件中设置用户的会话变量。


应用程序/缓存变量
应用程序和缓存变量可由任何用户访问,并且可以根据需要获取/设置。两者之间的唯一区别是缓存变量可以过期,这使得它们对于诸如数据库查询结果之类的事情很有用,这些结果可以在过期之前保留一段时间。
用法:

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

如果需要,您可以在 application_start 事件处理程序的 global.asax 文件中设置应用程序变量。


Web.Config
这可能是在应用程序中存储常量的首选方式,因为它们存储为“应用程序设置”并根据需要在 web.config 文件中进行更改,而无需重新编译站点。应用程序设置使用以下语法存储在文件的 区域中:

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

Web.config 值在代码中应被视为只读,并且可以在页面中使用此代码轻松访问:

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

静态变量
或者,您可以创建一个包含静态属性的类来保存变量,如下所示:

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

然后像这样访问它:

int myVar = SiteVariables.MyVariable;

我实际上在代码中使用了后两种解决方案的组合。我会将设置保留在 web.config 文件中,然后创建一个名为 ApplicationSettings 的类,该类在需要时使用静态属性从 web.config 读取值。

希望这有帮助

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

是你 2024-11-16 09:29:48

您可以创建一个带有静态成员的静态类:

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

然后您可以从站点中的任何位置调用 MyClass.MyVariable 来获取或设置该值。

请记住这与 PHP 之间的显着差异。在 PHP 中,您运行脚本并包含其他脚本以形成一个大脚本。在 ASP.NET 中,您将代码编译成程序集。图案不同。

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.

芯好空 2024-11-16 09:29:48

事实上,仅仅拥有像全局 php 变量静态类这样的东西就可以帮助你。但这真的是你想要的吗?因为众所周知,全局变量更难维护、可测试等......

可能您想将数据“放置”在某个地方并将其“带到”其他地方。

在 .NET 中有很多开箱即用的方法可以做到这一点。

您可以使用 SessionState 在与用户的会话持续期间存储某些内容
http://msdn.microsoft.com/en-us/library/ms178581.aspx

您可以使用 ViewState 作为短期...
http://msdn.microsoft.com/en-us/library/ms972976.aspx

您可以使用设置在 web.config 中设置变量
http://msdn.microsoft.com/en-us/library/b5ysx397.aspx

您可以使用配置文件来存储与用户相关的内容
http://msdn.microsoft.com/en-us/library/2y3fs9xs.aspx

您可以使用应用程序来存储更全局的内容
http://msdn.microsoft.com/en-us/library/ms178594.aspx

还有更多...
将此帖子视为 Karl Nicoll 帖子的“链接”。

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.

庆幸我还是我 2024-11-16 09:29:48

您可以通过内容页上的 Master 属性访问母版页上的属性(或字段或方法),这为您提供了对母版页的引用。不过,您需要首先将其转换为支持该属性的类型:

((Site)Master).MyVariable

并且 MyVariable 必须对内容页面可见,我认为是公共的或内部的。理想情况下,您应该转换为母版页的基本类型,而不是像上面的示例那样直接转换。

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.

不乱于心 2024-11-16 09:29:48

如果它是一个配置选项,请使用 web.config 文件应用程序设置部分

如果它是一个会话变量,则有一个会话对象

并且您还可以创建一个包含一些公共成员的静态类。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文