简体   繁体   English

如何在 ASP.net Web 应用程序中定义全局变量

[英]How to define a global variable in ASP.net web app

I have face a requirement,我有一个要求,

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 ...我希望客户端访问数据中心但不使用数据库,所以我希望我的网络应用程序可以保留一个包含数据的全局或应用程序会话变量,每个客户端都可以访问相同的数据......我尝试在 golabl 中声明,但似乎它只能存储字符串但其他...

how to solve this problem ?如何解决这个问题呢 ?

thanks.谢谢。

Another option of defining a global variable is by creating a static class with a static property:定义全局变量的另一种选择是创建一个具有静态属性的静态类:

public static class GlobalVariables
{
    public static string MyGlobalVariable { get; set; }
}

You can make this more complex if you are going to use this as a data store, but the same idea goes.如果您打算将其用作数据存储,您可以使这更复杂,但同样的想法也是如此。 Say, you have a dictionary to store your global data, you could do something like this:说,你有一个字典来存储你的全局数据,你可以做这样的事情:

public static class GlobalData
{
    private static readonly object _syncRoot = new object();
    private static Dictionary<string, int> _data;

    public static int GetItemsByTag(string tag)
    {
        lock (_syncRoot)
        {
            if (_data == null)
                _data = LoadItemsByTag();

            return _data[tag];
        }
    }

    private static Dictionary<string, int> LoadItemsByTag()
    {
        var result = new Dictionary<string, int>();

        // Load the data from e.g. an XML file into the result object.

        return result;
    }
}

To Share the data with all application users, you can use ASP.NET Application object.要与所有应用程序用户共享数据,您可以使用 ASP.NET Application 对象。 Given is the sample code to access Application object in ASP.NET:给出了在 ASP.NET 中访问 Application 对象的示例代码:

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;

Application["GlobalValueKey"] can be used anywhere in the whole application by any user. Application["GlobalValueKey"]可以被任何用户在整个应用程序的任何地方使用。 It will be common to all application users.它将对所有应用程序用户通用。

You can stuff data into the Application object if you want.如果需要,您可以将数据填充到Application对象中。 It isn't persistent across application instances, but that may sufficient.它不是跨应用程序实例持久的,但这可能就足够了。

(I'm not for a minute going to suggest this is a best practice, but without a clearer picture of the requirements, that's all I can suggest.) (我不会在一分钟内建议这是最佳实践,但没有更清晰的要求图,我只能建议。)

http://msdn.microsoft.com/en-us/library/system.web.ui.page.application.aspx http://msdn.microsoft.com/en-us/library/system.web.ui.page.application.aspx
http://msdn.microsoft.com/en-us/library/system.web.httpapplication.aspx http://msdn.microsoft.com/en-us/library/system.web.httpapplication.aspx

If you are using WebApplication or MVC just go to Global.asax (in WebSite project you need to add Global.asax from the add new item menu).如果您使用的是WebApplicationMVC只需转到Global.asax (在 WebSite 项目中,您需要从添加新项目菜单中添加Global.asax )。
I will explain to deploy two global variables for your web application:我将解释为您的 Web 应用程序部署两个全局变量:
Open the Global.asax file, then define your variable in Application_Start function as following:打开Global.asax文件,然后在Application_Start函数中定义变量,如下所示:

void Application_Start(object sender, EventArgs e)
{
   Application.Lock();
   Application["variable1"] = "Some Value for variable1";
   Application["variable2"] = "Some Value for variable2";
   Application.UnLock();
}


If you want to use that those global variables in aspx pages just need to call them like this:如果你想在 aspx 页面中使用那些全局变量,只需像这样调用它们:

<p>I want to call variable1 <%=Application["variable1"].ToString() %></p>
<p>I want to call variable1 <%=Application["variable2"].ToString() %></p>


But if you want to use that those global variables in server-side call'em like this:但是,如果您想像这样在服务器端 call'em 中使用那些全局变量:

protected void Page_Load(object sender, EventArgs e)
{
   string str1 = Application["variable1"].ToString();
   string str2 = Application["variable2"].ToString();
}

Note: You must be aware that these global variables are public to all users and aren't suitable for authentication jobs.注意:您必须注意,这些全局变量对所有用户都是公开的,不适用于身份验证作业。

您还可以使用Cache ,它具有设置过期时间/日期等优点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM