减少 ASP.NET 中 ViewState 大小的策略
我在页面中使用了“n”个服务器控件。现在我正在进行性能调整,我注意到我的 ViewState
太大,这使得我的页面变慢。
我知道 ViewState 大小可以通过 Gzip 压缩。有关减少 ASP.NET 中的 ViewState
的任何其他建议。我不想在 IIS 中执行此操作,因为我的 Web 应用程序托管在共享服务器上。
I use 'n' number of server controls in my page. Now I am into performance tuning and I've noticed that my ViewState
is too large and it makes my page slow.
I know ViewState
size can be compressed by Gzip. Any other suggestions for reducing ViewState
in asp.net. I don't want to do in IIS because my web application is hosted on a shared server.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
假设视图状态的大小是“缓慢”的主要原因,我想建议您从更全面的方法来解决它。
您有“n”个服务器控件,是否需要所有“n”个数字都是服务器控件而不仅仅是纯 HTML?
假设您确实需要所有“n”个,那么所有这些都需要启用视图状态吗?
这是一篇好文章(如果您还没有阅读),它提供了更多见解:
VIEWSTATE 大小最小化
Assuming the size of viewstate is the main cause of the 'slowness', I would like to suggestion you tackle it from a more holistic approach.
You have 'n' number of server controls, do you need all 'n' numbers to be server controls and not just plain HTML?
Say you really need all 'n' of them, do all of them need to have viewstate enabled?
Here is one good article (if you haven't already read) which provides more insights:
VIEWSTATE size minimization
EnableViewState = false;
应该成为你的朋友。假设您当前仅在需要的地方使用视图状态,您可以执行以下操作:
Labels
切换为Literals
,特别是当您在模板中使用它们时。标签需要更多的视图状态。EnableViewState = false;
should become your friend.Assuming you currently are using viewstate ONLY where you need you can do the following:
Labels
toLiterals
, especially if you are using them in templates. Labels take much more viewstate.仅当您需要记住回发之间页面的状态时才应使用视图状态。它用于防止对数据库的额外访问。因此,如果您的控件不需要这样做,请使用
EnableViewState = False
。如果页面上没有任何内容需要视图状态,您可以通过在Page
标记内添加EnableViewState = False
来禁用该页面的视图状态。如果您的服务器负担得起,您可能希望将数据传输到
会话
中。如果出于安全需要(视图状态不应包含敏感数据),或者您的视图状态保存了大量数据,请执行此操作。请注意,默认情况下,会话
存储在服务器的内存中。因此,如果您预计有许多并发用户,则不希望对大数据过多使用此功能。但是,您可以更改会话的存储位置(即另一台服务器)。Viewstates should only be used when you need to remember the state of the page between postbacks. It's used to prevent extra access to the database. So, if this isn't required in your control, use
EnableViewState = False
. If nothing on your page is going to need a viewstate, you can disable viewstates for that page by addingEnableViewState = False
within thePage
tag.If your server can afford it, you may want to transfer data into
Sessions
. Do this if necessary for security (viewstate should not contain sensitive data), or if your viewstate is holding a large amount of data. Be careful as, by default,Sessions
are stored in memory of the server. Therefore, you don't want to use this too much with large data if you're expecting many concurrent users. You can, however, change where the Session is stored (i.e. another server).将以下代码添加到生成大视图状态值的页面。
或者,可以将其添加到母版页中,从而无需在每个页面上添加。下面的代码允许将视图状态存储在会话中。
Add the following code to the page which generates large viewstate values.
Alternatively, this can be added to master page to eliminate the need of adding on each page. The below code allows the viewstate to be stored in session.
您应该做的第一件事就是在不需要的地方关闭视图状态。检查控件并确定哪些控件绝对需要打开视图状态。
The first thing you should do is turn off viewstate wherever you don't need it. Examine the controls and determine which ones absolutely need viewstate turned on.
正是我遇到的问题。我必须扩展 HiddenFieldPageStatePersister 并将视图状态保存在数据库中。我写了整篇文章来指导您。
http://ashishnangla.com/2011/07/21/reducing-size-of-viewstate-in-asp-net-webforms-by-writing-a-custom-viewstate-提供者-pagestatepersister-part-12/
Exactly the issue that I had. I had to extend the HiddenFieldPageStatePersister and save the viewstate in a database. I have written an entire article that will guide you.
http://ashishnangla.com/2011/07/21/reducing-size-of-viewstate-in-asp-net-webforms-by-writing-a-custom-viewstate-provider-pagestatepersister-part-12/