简体   繁体   中英

Global class variable for ASP.NET

I am building an ASP.NET Application which has 2 projects. One is a class library having BL Code. I want to create a public class instance variable from one of the classes in 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.

Create it in ViewState and wrap it in a property for easy of use. Something along the lines of:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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