回发不适用于 ASP.NET 路由(视图状态 MAC 验证失败) [英] Postback not working with ASP.NET Routing (Validation of viewstate MAC failed)
问题描述
我将 ASP.NET 3.5 SP1 System.Web.Routing 与经典的 WebForms 一起使用,如 http://chriscavanagh.wordpress.com/2008/04/25/systemwebrouting-with-webforms-sample/
I'm using the ASP.NET 3.5 SP1 System.Web.Routing with classic WebForms, as described in http://chriscavanagh.wordpress.com/2008/04/25/systemwebrouting-with-webforms-sample/
一切正常,我有自定义的 SEO 网址,甚至回发也有效.但有一种情况,回发总是失败,我得到一个:
All works fine, I have custom SEO urls and even the postback works. But there is a case where the postback always fails and I get a:
viewstate MAC 验证失败.如果此应用程序由 Web Farm 或集群托管,请确保配置指定相同的 validationKey 和验证算法.AutoGenerate 不能在集群中使用.
这里是重现错误的场景:
Here is the scenario to reproduce the error:
- 使用按钮创建标准网络表单 mypage.aspx
- 创建一个将a/b/{id}"映射到~/mypage.aspx"的路由
- 执行站点时,可以导航http://localhost:XXXX/a/b/something 页面有效.但是当您按下按钮时,您会收到错误消息.当路由只是a/{id}"时不会发生错误.
- Create a standard webform mypage.aspx with a button
- Create a Route that maps "a/b/{id}" to "~/mypage.aspx"
- When you execute the site, you can navigate http://localhost:XXXX/a/b/something the page works. But when you press the button you get the error. The error doen't happen when the Route is just "a/{id}".
好像和url中子路径的数量有关.如果至少有 2 个子路径,则视图状态验证失败.
It seems to be related to the number of sub-paths in the url. If there are at least 2 sub-paths the viewstate validation fails.
即使使用 EnableViewStateMac="false" 也会出现错误.
You get the error even with EnableViewStateMac="false".
有什么想法吗?是bug吗?
Any ideas? Is it a bug?
谢谢
推荐答案
我通过让我的视图用户控件继承自此类而不是 ViewUserControl
(它是一种补丁渲染视图).它对我有用,希望对你也有用.
I worked around this by having my view user control inherit from this class instead of ViewUserControl<T>
(it's kind of a patch for RenderView). It did the trick for me, hopefully it works for you too.
public class ViewUserControlWithoutViewState<T> : ViewUserControl<T> where T : class {
protected override void LoadViewState(object savedState) {}
protected override object SaveControlState() {
return null;
}
protected override void LoadControlState(object savedState) {}
protected override object SaveViewState() {
return null;
}
/// <summary>
/// extracted from System.Web.Mvc.ViewUserControl
/// </summary>
/// <param name="viewContext"></param>
public override void RenderView(ViewContext viewContext) {
viewContext.HttpContext.Response.Cache.SetExpires(DateTime.Now);
var containerPage = new ViewUserControlContainerPage(this);
ID = Guid.NewGuid().ToString();
RenderViewAndRestoreContentType(containerPage, viewContext);
}
/// <summary>
/// extracted from System.Web.Mvc.ViewUserControl
/// </summary>
/// <param name="containerPage"></param>
/// <param name="viewContext"></param>
public static void RenderViewAndRestoreContentType(ViewPage containerPage, ViewContext viewContext) {
string contentType = viewContext.HttpContext.Response.ContentType;
containerPage.RenderView(viewContext);
viewContext.HttpContext.Response.ContentType = contentType;
}
/// <summary>
/// Extracted from System.Web.Mvc.ViewUserControl+ViewUserControlContainerPage
/// </summary>
private sealed class ViewUserControlContainerPage : ViewPage {
// Methods
public ViewUserControlContainerPage(ViewUserControl userControl) {
Controls.Add(userControl);
EnableViewState = false;
}
protected override object LoadPageStateFromPersistenceMedium() {
return null;
}
protected override void SavePageStateToPersistenceMedium(object state) {}
}
}
这篇关于回发不适用于 ASP.NET 路由(视图状态 MAC 验证失败)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!