![_viewstate:](https://i-blog.csdnimg.cn/blog_migrate/94c5c576ba1c60473d7c78408804ab4f.png)
_viewstate:
![](https://i-blog.csdnimg.cn/blog_migrate/357038dd2c5f9cc2a1bbf9479d9229c0.png)
Scott Mitchell also has a good article on ViewState up on MSDN.
Scott Mitchell在MSDN上的ViewState上也有一篇不错的文章。
I talked to him before I blogged this and he agreed it was worth mentioning. I blogged about this issue before.
我在写博客之前先和他谈过,他同意值得一提。 我以前曾在博客上写过这个问题。
In his article Scott shows how one can override SavePageStateToPersistenceMedium and LoadPageStateFromPersistenceMedium to put ViewState somewhere else. A few snippets are below:
Scott在他的文章中展示了如何重写SavePageStateToPersistenceMedium和LoadPageStateFromPersistenceMedium来将ViewState放置在其他位置。 以下是一些摘要:
protected override void
SavePageStateToPersistenceMedium(object viewState)
{
LosFormatter los = new LosFormatter();
StringWriter writer = new StringWriter();
los.Serialize(writer, viewState);
StreamWriter sw = File.CreateText(ViewStateFilePath); <snip> } public string ViewStateFilePath
{
get
{ string folderName =
Path.Combine(Request.PhysicalApplicationPath,
"PersistedViewState");
string fileName = Session.SessionID + "-" +
Path.GetFileNameWithoutExtension(Request.Path).Replace("/",
"-") + ".vs";
return Path.Combine(folderName, fileName); }
}In this example code (that you shouldn't copy/paste into production :) ) you see that he's redirecting ViewState to serialize to a file with a name like ASPNET23234094498230948320492834-myfile-default.aspx.vs.
受保护的覆盖无效SavePageStateToPersistenceMedium(对象viewState) { LosFormatter los =新的LosFormatter(); StringWriter writer = new StringWriter(); los.Serialize(writer,viewState); StreamWriter sw = File.CreateText( ViewStateFilePath ); <snip> } 公共字符串ViewStateFilePath { 得到{ 字符串folderName = Path.Combine(Request.PhysicalApplicationPath, “ PersistedViewState”); 字符串fileName = Session.SessionID +“-” + Path.GetFileNameWithoutExtension(Request.Path).Replace(“ /”, “-”)+“ .vs”; 返回Path.Combine(folderName,fileName); } } 在此示例代码中(您不应将其复制/粘贴到生产环境中:)),您看到他将ViewState重定向为序列化为名称为ASPNET23234094498230948320492492834-myfile-default.aspx.vs的文件。
The problem (an edge case certainly, but still a problem) with this approach is that it doesn't support multiple browser windows on the same machine hitting the same page.
这种方法的问题(当然是一个极端的情况,但仍然是一个问题)是,它不支持同一台计算机上访问同一页面的多个浏览器窗口。
Remember where ViewState is stored by default - it's stored with the requested page instance (in the HTML). Using the ASP.NET SessionID in the filename scopes the state to the user and adding the file name reduces scope to the Page Declaration, but not the actual request instance.
记住默认情况下ViewState的存储位置-它与请求的页面实例一起存储(在HTML中)。 在文件名中使用ASP.NET SessionID可以将状态范围限制在用户范围内,而添加文件名则可以将范围限制在页面声明范围内,而不是实际请求实例范围内。
Fortunately, Scott Mitchell wisely aludes to a solution in his article when he says:
幸运的是,斯科特·米切尔(Scott Mitchell)在他的文章中明智地要求他提供一种解决方案:
Note One workaround would be to use a globally unique identifier (GUID) as the file name for the persisted view state, saving this GUID in a hidden form field on the ASP.NET Web page. This approach, unfortunately, would take quite a bit more effort than using the
SessionID
/ URL scheme, since it involves injecting a hidden form field into the Web Form. For that reason, I'll stick to illustrating the simpler approach for this article.注意一种解决方法是使用全局唯一标识符(GUID)作为持久化视图状态的文件名,将此GUID保存在ASP.NET网页上的隐藏表单字段中。 不幸的是,这种方法比使用
SessionID
/ URL方案要花费更多的精力,因为它涉及将隐藏的表单字段注入Web表单。 因此,我将坚持为本文说明更简单的方法。
I spoke with Scott, and he agreed that for this solution to be more ideal one would have to implement a solution using a GUID. Otherwise, be aware that you may run into flakey concurrence bugs where pages step on each other's ViewState.
我与Scott谈过,他同意,要使该解决方案更加理想,就必须使用GUID来实现一个解决方案。 否则,请注意,您可能会遇到网页并排出现在彼此的ViewState上的共生错误。
翻译自: https://www.hanselman.com/blog/important-note-replacing-the-default-viewstate-persistance-behavior
_viewstate: