ASP.NET的全局类变量 [英] Global class variable for ASP.NET

查看:42
本文介绍了ASP.NET的全局类变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个包含2个项目的ASP.NET应用程序。一个是具有BL代码的类库。我想从BL中的一个类创建一个公共类实例变量。此类实例变量是为了避免在每个请求上加载所有数据,这使得我的应用程序对每个请求的响应变慢。全局类变量将在page_Load加载数据,并保持到用户重定向到另一个页面。

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. The global class variable will load data at page_Load, and remain until the user redirects to another page.

推荐答案

不,它不会。 Web应用程序不像Windows应用程序:它不会一直运行等待用户做某事。



每次用户执行时都会发生Page_Load与服务器对话的任何事情:它不会发生一次,然后再发生,直到用户更改页面。每次用户按下按钮时,都会调用Page_load。



除非您在cookie或会话中保留您的信息,否则只要服务器感觉到它就会被丢弃喜欢它。



请看这里的首发: MSDN:ASP.NET页面生命周期概述 [ ^ ]

当你掌握了它时,开始查看你的BL代码并查看你需要多少每次用户想做某事时做。大多数情况下,如果您的应用程序运行缓慢,那是因为您没有思考正确并且已经加载了所有内容而不是考虑用户实际需要的内容。
No, it won't. a web application is not like a Windows application: it doesn't run all the time waiting for the user to do something.

Page_Load occurs every time that the user does anything which "talk" to the server: it doesn't occur once and then never again until the user changes pages. Every time the user presses a button, Page_load is called.

And unless you preserve your information in cookies or the Session, it can and will be discarded whenever the server feels like it.

See here for starters: MSDN: ASP.NET Page Life Cycle Overview[^]
And when you have grasped that, start looking at your BL code and seeing how much of that you need to do each time the user wants to do something. Most times, if your app is slow, it's because you are not "thinking right" and have loaded everything instead of considering what the user actually needs.


这篇关于ASP.NET的全局类变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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