Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在asp.net中使用常用变量?_Asp.net_.net_Vb.net - Fatal编程技术网

如何在asp.net中使用常用变量?

如何在asp.net中使用常用变量?,asp.net,.net,vb.net,Asp.net,.net,Vb.net,我是.net新手,我通常有一个公共文件,其中包含用户名、服务器、密码、IP和php和asp classic中的固定整数信息,我以前将这些信息包含在所有文件中,以便我可以在任何页面中使用它们。因为我知道.net没有包含函数。我怎样才能在asp.net中做到这一点 谢谢您不必使用包含文件在站点上共享变量。在.Net中,您通常会在web.config文件的AppSettings部分存储这样的值 然后,您可以使用以下代码访问这些值: string username = WebConfigurationM

我是.net新手,我通常有一个公共文件,其中包含用户名、服务器、密码、IP和php和asp classic中的固定整数信息,我以前将这些信息包含在所有文件中,以便我可以在任何页面中使用它们。因为我知道.net没有包含函数。我怎样才能在asp.net中做到这一点


谢谢

您不必使用包含文件在站点上共享变量。在.Net中,您通常会在web.config文件的
AppSettings
部分存储这样的值

然后,您可以使用以下代码访问这些值:

string username = WebConfigurationManager.Appsettings["Username"];

严格地说,如果使用应用程序配置(如DB连接字符串),则这些配置对于web应用程序放在web.config中,对于其他应用程序放在app.config中

还有一些设置文件

查看
ConfigurationManager


除此之外,您必须记住“全局变量是坏的mmmkay”,除了各种配置文件之外,最接近它们的是公共类和属性。

除了Digbyswift和webconfig(我也建议)提到的应用设置之外,您还可以使用一个模块:

Module Module2
    Public MyGlobalInteger As Integer = 0
End Module
然后可以从任何地方调用它,如:

    Dim someOtherNumber As Integer = MyGlobalInteger + 5
不过,您可能希望将其设置为只读,具体取决于您的使用情况