回传与ASP.NET路由(视图状态MAC验证失败)工作 [英] Postback not working with ASP.NET Routing (Validation of viewstate MAC failed)

查看:106
本文介绍了回传与ASP.NET路由(视图状态MAC验证失败)工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用ASP.NET 3.5 SP1 System.Web.Routing经典的WebForms,如<一个描述href=\"http://chriscavanagh.word$p$pss.com/2008/04/25/systemwebrouting-with-webforms-sample/\">http://chriscavanagh.word$p$pss.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/

所有工作正常,我有自定义的网址,搜索引擎优化,甚至回发的作品。 但是,就是回发总是失败情况下,我得到一个:

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:

视图状态MAC验证失败。如果此应用程序由Web场或群集承载,确保配置指定了相同的validationKey和验证算法。自动生成不能在集群中使用。

下面是重现错误的场景:

Here is the scenario to reproduce the error:


  1. 一个按钮创建一个标准的Web窗体mypage.aspx

  2. 创建一个路由映射A / B /(编号),以〜/ mypage.aspx

  3. 当你执行的网站,你可以浏览的http://本地主机:XXXX / A / B / 东西的页的作品。但是,当你preSS的按钮,你得到的错误。错误doen't当路由就是一个/(编号)发生。

  1. Create a standard webform mypage.aspx with a button
  2. Create a Route that maps "a/b/{id}" to "~/mypage.aspx"
  3. 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 =假。

You get the error even with EnableViewStateMac="false".

任何想法?它是一个错误吗?

Any ideas? Is it a bug?

感谢

推荐答案

我工作围绕这通过让我的看法用户控件从这一类,而不是继承 ViewUserControl&LT; T&GT; (它是一种为的RenderView补丁)。这为我做的伎俩,希望它为你工作了。

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆