简体   繁体   English

ASP.NET的全局类变量

[英]Global class variable for ASP.NET

I am building an ASP.NET Application which has 2 projects. 我正在构建一个具有2个项目的ASP.NET应用程序。 One is a class library having BL Code. 一个是具有BL代码的类库。 I want to create a public class instance variable from one of the classes in BL. 我想从BL中的一个类创建一个公共类实例变量。 This class instance variable is to avoid loading all the data on each request which makes my application to respond slow for each request. 该类实例变量是为了避免在每个请求上加载所有数据,这会使我的应用程序对每个请求的响应速度变慢。 How to make the global class variable load data at page_Load, and keep it until the user redirects to another page. 如何使全局类变量在page_Load处加载数据,并保留该数据,直到用户重定向到另一个页面为止。

Create it in ViewState and wrap it in a property for easy of use. 在ViewState中创建它,并将其包装在一个属性中,以方便使用。 Something along the lines of: 类似于以下内容:

public MyClass MyObj {
    get {
        if (ViewState["MyObj"] == null){
             ViewState["MyObj"] = new MyClass();
        }
        return ViewState["MyObj"]; 
    }
    set {
        ViewState["MyObj"] = value;
    }
}

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

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