C# 如何获取构建的、编码的 ViewState 的值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1010/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the value of built, encoded ViewState?
提问by Rex M
I need to grab the base64-encoded
representation of the ViewState
. Obviously, this would not be available until fairly late in the request lifecycle, which is OK.
我需要获取base64-encoded
的表示ViewState
。显然,这在请求生命周期的相当晚之前是不可用的,这是可以的。
For example, if the output of the page includes:
例如,如果页面的输出包括:
<input type="hidden" name="__VIEWSTATE"
id="__VIEWSTATE" value="/wEPDwUJODU0Njc5MD...==" />
I need a way on the server-side to get the value "/wEPDwUJODU0Njc5MD...=="
我需要在服务器端获取值的方法 "/wEPDwUJODU0Njc5MD...=="
To clarify, I need this value when the page is being rendered, not on PostBack. e.g. I need to know the ViewState value that is being sentto the client, not the ViewState I'm getting back from them.
澄清一下,我需要在页面呈现时使用此值,而不是在 PostBack 上。例如,我需要知道发送给客户端的 ViewState 值,而不是我从他们那里得到的 ViewState。
采纳答案by Jeff Atwood
Rex, I suspect a good place to start looking is solutions that compress the ViewState-- they're grabbing ViewState on the server before it's sent down to the client and gzipping it. That's exactly where you want to be.
Rex,我怀疑一个开始寻找的好地方是压缩 ViewState 的解决方案——他们在服务器上抓取 ViewState,然后将其发送到客户端并对其进行 gzip压缩。这正是您想要的地方。
回答by Yaakov Ellis
See this blog postwhere the author describes a method for overriding the default behavior for generating the ViewState and instead shows how to save it on the server Session object.
请参阅此博客文章,其中作者描述了一种用于覆盖生成 ViewState 的默认行为的方法,而是展示了如何将其保存在服务器 Session 对象上。
In ASP.NET 2.0, ViewState is saved by a descendant of PageStatePersister class. This class is an abstract class for saving and loading ViewsState and there are two implemented descendants of this class in .Net Framework, named HiddenFieldPageStatePersister and SessionPageStatePersister. By default HiddenFieldPageStatePersister is used to save/load ViewState information, but we can easily get the SessionPageStatePersister to work and save ViewState in Session object.
在 ASP.NET 2.0 中,ViewState 由 PageStatePersister 类的后代保存。该类是一个用于保存和加载 ViewsState 的抽象类,在 .Net Framework 中有两个实现的该类的后代,名为 HiddenFieldPageStatePersister 和 SessionPageStatePersister。默认情况下,HiddenFieldPageStatePersister 用于保存/加载 ViewState 信息,但我们可以轻松地让 SessionPageStatePersister 工作并将 ViewState 保存在 Session 对象中。
Although I did not test his code, it seems to show exactly what you want: a way to gain access to ViewState code while still on the server, before postback.
尽管我没有测试他的代码,但它似乎准确地显示了您想要的内容:一种在回发之前仍在服务器上访问 ViewState 代码的方法。
回答by Dave Anderson
I enabled compression following similar articles to those posted above. The key to accessing the ViewState before the application sends it was overriding this method;
我在上面发布的类似文章之后启用了压缩。在应用程序发送它之前访问 ViewState 的关键是覆盖这个方法;
protected override void SavePageStateToPersistenceMedium(object viewState)
You can call the base method within this override and then add whatever additional logic you require to handle the ViewState.
您可以在此覆盖中调用基本方法,然后添加处理 ViewState 所需的任何其他逻辑。