回发不使用ASP.NET路由(视图状态MAC验证失败)

5 viewstate asp.net-mvc routing mac-address

我正在使用ASP.NET 3.5 SP1 System.Web.Routing与经典WebForms,如http://chriscavanagh.wordpress.com/2008/04/25/systemwebrouting-with-webforms-sample/中所述

一切正常,我有自定义SEO网址甚至回发作品.但有一种情况是回发总是失败,我得到一个:

验证视图状态MAC失败.如果此应用程序由Web场或群集托管,请确保配置指定相同的validationKey和验证算法.AutoGenerate不能在群集中使用.

以下是重现错误的方案:

  1. 使用按钮创建标准webform mypage.aspx
  2. 创建一个将"a/b/{id}"映射到"〜/ mypage.aspx"的路线
  3. 当您执行该站点时,您可以导航http:// localhost:XXXX/a/b /页面工作的内容.但是当你按下按钮时会出现错误.当Route只是"a/{id}"时,不会发生错误.

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

即使使用EnableViewStateMac ="false",您也会收到错误.

有任何想法吗?这是一个错误吗?

谢谢

Mau*_*fer 8

我通过让我的视图用户控件从这个类继承而不是ViewUserControl<T>(它是RenderView的补丁)来解决这个问题.它为我做了伎俩,希望它也适合你.

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) {}
    }
}
Run Code Online (Sandbox Code Playgroud)

我不久前在博客上发表过这篇文章.


Mis*_*oon -3

您使用 safari 作为浏览器吗?如果是这样,那么这可能是大浮动的问题。去掉那个浮动,一切都会正常。