为什么要使用 ASP.NET 的 ViewState 存储对象而不是 Session 存储对象?

发布于 2024-07-14 12:23:37 字数 377 浏览 5 评论 0原文

除了因为会话存储对于多个页面而言是会话全局的之外,您为什么要使用视图状态来保存值?

除了一些小的查询字符串(例如值)之外,从客户端到服务器来回发送任何类型的信息似乎都有点荒谬。 我的意思是,仅仅为了存储目的就浪费了带宽(!)。 会话虽然跨多个页面全局,但似乎是视图状态的完全优越的替代方案。

特别是对于 ASP.NET Ajax 控件和变体,跟踪所有这些不同控件和 html 元素的各种状态和变量的视图状态可能很快就会变得臃肿。

但是为什么页面变量和对象有视图状态存储呢?

也许我错过了页面视图状态存储的另一个很好的用法,有人知道吗?

谢谢阅读!

编辑:每个人都有一个很好的答案,很抱歉,如果我没有选择你的答案。

Other than because session storage is session-global to more than one page, why would you ever want to use the viewstate to hold values?

It seems kind of ridiculous to send any kind of information other than a few small query string like values, back and forth from the client to server. I mean what a waste of bandwidth(!), simply for storage purposes. The session, while global across multiple pages, seems like a completely superior alternative to the viewstate.

Especially with asp.net ajax controls and variants, the viewstate could quickly become bloated tracking the various states and variables of all those different controls and html elements.

But then why is there viewstate storage for page variables and objects at all?

Maybe I'm missing another great usage for the page's viewstate storage, does anyone know something out there?

Thanks for reading!

EDIT: Everyone had a great answer, sorry if I didn't pick yours.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

何以畏孤独 2024-07-21 12:23:37

会话用完,视图状态没有 - 您可以在一小时后返回,您的视图状态仍然可用。 当您在网站上后退/前进、会话更改时,视图状态也始终可用。

Sessions run out, Viewstate does not - you can go back an hour later and your viewstate will still be available. Viewstate is also consistently available when you go back/forward on the website, Session changes.

衣神在巴黎 2024-07-21 12:23:37

Viewstate 或 Session 的全部目的是将网络从无状态系统转变为动态的、定制的体验。 当用户请求页面时,可以从用户上次中断的地方恢复的唯一方法是记住服务器或用户客户端上的状态。

Viewstate 是一种在客户端记住用户状态的机制。
Session是一种在服务器上记住用户状态的机制。

Viewstate 是一种临时存储机制。 使用 viewstate 的控件将其状态作为隐藏输入呈现到 html 页面中。 为了防止篡改,它被签名。 但是,它没有加密,因此您可能希望避免在其中放置任何敏感内容。 Viewstate 对于您想要跨一系列多个请求(页面加载)进行发布的情况非常有用。 例如,当表单因用户输入了错误的电子邮件地址或其他内容而无法验证时,您希望将表单恢复为用户提交之前的状态。 这样做的缺点是 viewstate 是一个饥饿的野兽,可以轻松地将页面大小增加 30-50%。

另一方面,会话存储在服务器上。 客户端获得一个令牌,告诉服务器哪个内存块是他们的。 这比视图状态安全得多,因为数据不会一遍又一遍地重新传输给用户。 但也有一些权衡。 您的服务器可能会耗尽内存。 或者,如果会话中断,用户可能会丢失数据。

一般来说,没有可供使用的“正确”答案。 这一切都与您想要实现的目标有关。

大多数与控件相关的事情都应该使用 Viewstate。 但是,如果您正在处理敏感信息,请考虑 Session。 如果您有特定页面集的数据,请使用视图状态。 如果您在用户访问您的网站的整个过程中都需要这些数据,请考虑会话。

The whole reason for Viewstate or Session is to turn the web from a stateless system into a dynamic, customized experience. When a user requests a page, the only way you can resume where the user left off in their experience is to remember the state either on the server or on the user's client.

Viewstate is a mechanism for remembering the user's state on the client.
Session is a mechanism for remembering the user's state on the server.

Viewstate is a transient storage mechanism. Controls that use viewstate have their state rendered into the html page as a hidden input. To prevent tampering, it is signed. It is not encrypted, however, so you probably want to avoid putting ANYTHING sensitive in there. Viewstate is useful for situations where you want to post across series of multiple requests (page loads). An example of this is when a form doesn't validate because maybe the user entered a bad email address or something, and you want to restore the form as it was before the user submitted. The downsides to this is that viewstate is a hungry beast and can easily add 30-50% to page size.

Session, on the other hand, is stored on the server. The client gets a token that tells the server which block of memory is theirs. This can be much more secure than viewstate because the data isn't being retransmitted to the user over and over. There are trade-offs though. Your server can run out of memory. Or, the user could lose the data if their session is disrupted.

Generally, there's no "right" answer on which to use. It is all about what you are trying to accomplish.

Most things to do with controls should use Viewstate. IF you're dealing with sensitive information however, consider Session. If you have data that is for a specific set of pages, use viewstate. If it is data that you will need throughout a user's visit on your site, considier Session.

无声情话 2024-07-21 12:23:37

例如,当您的应用程序可能在计算机场中运行并且您无法配置会话以使用 sql server 时。(或者使用 sql server 对性能影响太大)

For example when your application might be running in a computer farm and you can't configure session to use sql server.( Or using sql server is too much of a performance hit)

述情 2024-07-21 12:23:37

ViewState 和 Session 具有不同的范围。 ViewState 旨在在“回发”期间存储或多或少的瞬态数据,而 session 用于保存关键会话状态数据。 我建议使用 ViewState 来获取与特定“页面会话”相关的状态。

如果您不喜欢 ViewState 的正常行为,那么编写自己的 PageStatePersister 并让该对象执行持久化非常简单,例如使用 session 或 Memcached 之类的东西。 然后您可以完全覆盖默认的持久性机制。

然后,好处是您可以无缝地继续使用 .NET Framework 中的标准 Web 控件,这些控件都将 ViewState/ControlState 用于此类数据,而不会导致 ViewState 膨胀。 服务器内存持久性机制可能非常有效。

ViewState and Session have different scopes. ViewState is designed to store more or less transient data, during "postbacks", while session is used to save critical session state data. I recommend using ViewState for state related to a specific "page session".

If you don't like the normal behavior of ViewState, it's pretty simple to write your own PageStatePersister and let this object perform persistence, for instance using session, or something like Memcached. You can then completely override the default persistence mechanism.

Then, the good thing is you can seamlessly continue to use standard web controls in the .NET Framework, which will all use ViewState/ControlState for this type of data, without bloating the ViewState. A server memory persistence mechanism could be very efficient.

月亮坠入山谷 2024-07-21 12:23:37

并不是对您问题的直接回答,但它可能会解决您的问题。

您可以在服务器端存储视图状态,从而消除客户端的负载。

创建一个继承page的类,并重写PageStatePersister。
http://msdn.microsoft.com/en-我们/library/system.web.ui.sessionpagestatepersister.aspx

 public class RussPage : Page
    {
         protected override PageStatePersister PageStatePersister
        {
            get
            {
                return new SessionPageStatePersister(Page);
            }
        }
    }

Not really a direct answer to your question, but it may resolve your issue.

You can store viewstate server side, eliminating the payload for the client.

Create a class the inherits page, and override the PageStatePersister.
http://msdn.microsoft.com/en-us/library/system.web.ui.sessionpagestatepersister.aspx

 public class RussPage : Page
    {
         protected override PageStatePersister PageStatePersister
        {
            get
            {
                return new SessionPageStatePersister(Page);
            }
        }
    }
最好是你 2024-07-21 12:23:37

ViewState 本质上只是一个隐藏的输入,必须上传到服务器并随每个请求进行解析。 该字段通常是自动填充的,通常程序员幸福地没有意识到,并且可能会变得相当大。 对于许多站点来说这是一个问题,因为即使是宽带用户的上行带宽也非常有限。

在所有用户都可以高速 LAN 访问服务器但可用于保存会话数据的 RAM 有限的 Intranet 站点上,这可能更有意义。

ViewState is essentially just a hidden input that must be uploaded to the server and parsed with each request. This field is typically populated automatically, often with the programmer blissfully unaware, and can grow quite large. For many sites that presents a problem, because even broadband users have very limited upstream bandwidth.

On intranet sites where all the users have high-speed LAN access to the server but the ram available for holding session data is limited, it may make more sense.

扛起拖把扫天下 2024-07-21 12:23:37

不是您问题的答案,但您的假设之​​一是不正确的。

会话 ID 可以在 URL 中传递。 会话不需要 cookie。

http://msdn.microsoft.com/en-us/library/aa479314。 ASPX

<sessionState cookieless="true" />

Not an answer to your question but one of your assumptions is incorrect.

Session IDs can be passed in the URL. Session does not require cookies.

http://msdn.microsoft.com/en-us/library/aa479314.aspx

<sessionState cookieless="true" />
宁愿没拥抱 2024-07-21 12:23:37

您正在开发一个应用程序,其中视图状态膨胀在大多数情况下不是问题,那么最好将页面特定数据存储在视图状态中,因为它可以帮助您的服务器更好地执行。 如果你对会话或任何缓存过于疯狂,那么你对自己的伤害可能比帮助自己还要大。

You are doing an app where viewstate bloat, for the most part, is not an issue, then it's better to store page specific data in the viewstate because it helps your server perform better. If you go crazy with session, or any caching, for that matter, you can hurt yourself more then you help yourself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文