使用ViewState和hiddenfield哪个最好
我有一个页面,我想在回发之间维护对象的值。 我在想两种方法来保持物品的价值
- 将值存储在 View Sate 中
- 将值存储在隐藏字段中
这是根据性能使用的最佳选项
I have a page in which I want to maintain the value of object between post backs.
I am thinking of two ways to maintain the value of objects
- Store the value in View Sate
- Store the value in hidden field
which is best option to use based on performance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您不需要需要在客户端脚本中引用它,请查看状态。如果您这样做,则为隐藏字段。
另请考虑,如果数据敏感,则默认情况下会对 Viewstate 进行加密,而默认情况下隐藏字段会将其存储为任何知道如何查看源的人都可见的纯文本。
编辑
根据 @Andrew Hare 关于他自己答案的注释,我正在编辑此内容。这是一个值得注意的重要区别。我讨厌有人根据我的监督认为他们使用 Viewstate 是“安全的”。
Viewstate 默认情况下不加密,它存储为 Base-64 编码。它可以很容易地解码,因此使用 Viewstate 因为默认情况下已加密是无效的。它比纯文本更好,但不适用于任何有能力通过谷歌搜索“解密 Viewstate”或“解码 Viewstate”的人。
因此,不要依赖 Viewstate 来保护客户端代码中的隐藏信息。
此处介绍了如何正确加密。 (但也警告性能问题)。
Viewstate if you don't need to reference it in client side script. A Hidden field if you do.
Also consider that if the data is sensitive, the Viewstate is encrypted by default, whereas the hidden field, by default, stores it as plain text visible to anyone who knows how to view source.
Edit
Per @Andrew Hare's note on his own answer, I'm editing this. It's an important enough distinction to note. I'd hate for someone to think they were "safe" using the Viewstate based on my oversight.
The Viewstate is NOT encrypted by default, it's stored as Base-64 encoding. It can be decoded fairly easily, so using the Viewstate because it's encrypted by default is not valid. It's better than plain text, but not to anyone with the ability to google "decrypt Viewstate" or "decode Viewstate".
So don't rely on the Viewstate to protect your hidden information in client side code.
An article here tells how to encrypt it properly. (but also warns about performance issues).
这并不重要,因为 ViewState 本身存储在隐藏输入中。使用对您来说更容易的一种。如果由我决定,我会选择 ViewState,因为 ASP.NET 运行时将为您处理对象的序列化和反序列化。
It doesn't really matter since ViewState is itself stored in a hidden input. Use whichever one is easier for you. If it were up to me I would choose ViewState since the ASP.NET runtime will handle the serialization and deserialization of your objects for you.
我喜欢 ViewState - 它更难破解 - 一个讨厌的人可以轻松地将您的页面提交给您,并在您的隐藏字段中包含错误的数据
I like ViewState - it is much harder to hack - a nasty person could easily submit your page to you with bad data in your hidden fields
您想将其存储在视图状态中。隐藏字段可以在浏览器上更新,因为它们旨在存储可以在客户端操作的信息。视图状态将由 asp.net 进行验证以防止篡改,您必须自己对隐藏字段进行验证。
You want to store it in the View State. Hidden fields can be updated on the browser, as they are meant to store information that can be manipulated on the client side. The view state will be validated by asp.net against tampering, where you will have to do that with the hidden field yourself.