使用 URL 路由的视图状态 MAC 验证在 ASP.NET 4.0 中失败

本文关键字:NET ASP 失败 验证 路由 URL 视图状态 MAC 使用 | 更新日期: 2023-09-27 17:55:08

我得到了臭名昭著的错误

Validation of viewstate MAC failed. If this application is hosted by a Web Farm or cluster, ensure that <machineKey> configuration specifies the same validationKey and validation algorithm. AutoGenerate cannot be used in a cluster.

经过广泛的搜索,我找不到任何可以解决我的特定问题的东西。 我在 .NET 4 中使用 URL 路由,但我没有使用 MVC。 我发现的所有类似的问题/解决方案(由于 URL 路由)都在 MVC 中提供解决方案,但不在常规 ASP.NET Web 窗体中提供解决方案。

这里的堆栈溢出问题指出:

这似乎与 url 中的子路径数量有关。如果至少有 2 个子路径,则视图状态验证将失败。

在测试这个理论时,我能够通过简单地改变我的路线来提供一个快速而肮脏的解决方法

Module/Search/Results

Module/SearchResults

这暂时解决了这个问题,但是,该帖子中的任何答案都没有帮助。

我发现一些有趣的事情,该错误可以在IE9中重现,但在Chrome 24.0.1313.52m中则不能重现。 这让我相信此错误是特定于浏览器的,所以我尝试为我的网站设置 NoCache,但无济于事,Chrome 仍然有效,IE9 仍然抛出了 ViewState 错误。

为了重现该问题,以下是我的环境的一些关键因素:

Windows 7 64-bit
IIS 6.1 SP 1
Local Web Project (VS 2010)
Nested Master Pages (3 levels)
URL Routing (defined in Global.asax)
Page contains a GridView with a TemplateField.  
GridView has PagerSettings defined.
Links causing the error are within the TemplateField.
Links outside of the GridView do not cause the error.

重现步骤如下:

User browses to page "~/Module/Search/Results" (from link or directly doesn't matter)
User clicks on a button that posts back and causes a redirect to another page "~/Module/DoSomething"
User presses the Back button in the browser which takes them back to "~/Module/Search/Results"
User then proceeds to press a button that causes a post-back which immediately causes the error.

更新:添加了有关页面布局的关键因素中的信息。

如何为所有浏览器解决此问题,以便我可以在网页的URL中拥有2个或更多子路径,而不会在用户返回页面然后生成另一个回发时导致错误?

使用 URL 路由的视图状态 MAC 验证在 ASP.NET 4.0 中失败

我想通了!

在尝试解决我收到错误的问题时

Microsoft JScript 运行时错误:"WebForm_FireDefaultButton"未定义

我找到一篇文章说我需要将以下内容添加到我的路由表的顶部

路线。Ignore("{resource}.axd/{*pathInfo}");

这似乎已经解决了我正在寻找的问题和这个问题中的问题。