Postback does not work with ASP.NET routing (failed to validate viewstate MAC address)

I am using 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/

Everything works fine , I have custom urls and even reverse work. But there is a case where postback always fails , and I get:

Failed to verify viewstate MAC address. If this application is hosted by a web farm or cluster, make sure the configuration validationKey and validation are specified in the configuration. AutoGenerate cannot be used in a cluster.

Here is the script to reproduce the error:

  • Create a standard mypage.aspx web form using the button
  • Create a route that maps "a / b / {id}" to "~ / mypage.aspx"
  • When you run the site, you can navigate to http: // localhost: XXXX / a / b / something . But when you click the button, you get an error. An error will not occur when the Route is simply "a / {id}".

This seems to be related to the number of subpaths in the url. If there are at least two paths, validation verification is not performed.

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

Any ideas? This is mistake?

thanks

+5
source share
6 answers

, 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) {}
    }
}

I .

+8

asp.net mvc beta. . asp.net mvc asp: button home.aspx , f5, . , . mvc, , ViewUserControl site.master, <% Html.RenderPartial( "LoginUserControl" ); % > , click .

, enableViewStateMac = "false" enableEventValidation = "false" viewStateEncryptionMode = "Never" .

mvc ViewUserControl

public virtual void RenderView (ViewContext viewContext) {

// TODO: Remove this hack. Without it, the browser appears to always load cached output
viewContext.HttpContext.Response.Cache.SetExpires(DateTime.Now);
**ViewUserControlContainerPage containerPage = new ViewUserControlContainerPage(this);**
// Tracing requires Page IDs to be unique.
ID = Guid.NewGuid().ToString();
containerPage.RenderView(viewContext);

}

ViewUserControlContainerPage: ViewPage {

public ViewUserControlContainerPage(ViewUserControl userControl) {
    Controls.Add(userControl);
}

}

ViewUserControl , . , , container.enableViewStateMac false, . , - Microsoft mvc.

+1

I had the same problem, I had a scammer

<form></form>

Tags, as soon as I deleted them from my page, the error no longer appeared.

0
source

Just try clearing cookies on your local computer. Had the same problem and it helped.

0
source

Do you use safari as a browser? if so, then this is likely to be a problem with a large float. Remove this float and everything will work fine.

-2
source

All Articles