如何定义在ASP.net web应用程序的全局变量 [英] How to define a global variable in ASP.net web app

查看:203
本文介绍了如何定义在ASP.net web应用程序的全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有面子的要求,

我想客户端访问数据中心,但不使用数据库,所以我想我的web应用程序可以保留全局或应用程序会话变量,包含数据,每一个客户端可以访问相同的数据...我试图声明在golabl,但它似乎只能存储字符串,但其他人...

I want client access a data center but without use database , so I want my web app can retain a global or Application session variable, that contains the data, every client can access the same data... I am try to declare in golabl, but seem it only can store String but others ...

如何解决这个问题呢?

how to solve this problem ?

感谢。

推荐答案

与大家分享所有的应用程序的用户数据,您可以使用ASP.NET Application对象。由于是样品code访问ASP.NET Application对象:

To Share the data with all application users, you can use ASP.NET Application object. Given is the sample code to access Application object in ASP.NET:

Hashtable htblGlobalValues = null;

if (Application["GlobalValueKey"] != null)
{
    htblGlobalValues = Application["GlobalValueKey"] as Hashtable;
}
else
{
    htblGlobalValues = new Hashtable();
}

htblGlobalValues.Add("Key1", "Value1");
htblGlobalValues.Add("Key2", "Value2");

this.Application["GlobalValueKey"] = htblGlobalValues;

应用[GlobalValueKey] 可以在任何地方通过任何用户的整个应用程序中使用。这将是适用于所有应用程序的用户。

Application["GlobalValueKey"] can be used anywhere in the whole application by any user. It will be common to all application users.

这篇关于如何定义在ASP.net web应用程序的全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆