views:

698

answers:

4

I am randomly encountering the following error:

Message: Invalid viewstate. Client IP: xx.xxx.xxx.xx Port: 2324 User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.1; InfoPath.2; .NET CLR 3.5.30729; .NET CLR 3.0.30618) ViewState:

Stack Trace:

Message: Invalid length for a Base-64 char array. Stack Trace: at System.Convert.FromBase64String(String s) at System.Web.UI.ObjectStateFormatter.Deserialize(String inputString) at System.Web.UI.ObjectStateFormatter.System.Web.UI.IStateFormatter.Deserialize(String serializedState) at System.Web.UI.Util.DeserializeWithAssert(IStateFormatter formatter, String serializedState) at System.Web.UI.HiddenFieldPageStatePersister.Load()

Can anyone point me to how I go about debugging this?*

A: 

This guy also ran into same error, I recommend you to take a look at it.

Invalid ViewState

The solution to this problem may be to set a flag in machine.config to prevent ASP.NET to generate a new key everytime an Application starts.

Braveyard
A: 

There are a number of reasons for getting this error. On the CLR v1.1 there was an issue with IIS that caused that. See kb article. However since you're 3.5 I assume that's not the case.

There are other suggestions for possible issues hee:

http://stackoverflow.com/questions/576990/should-i-ignore-the-occasional-invalid-viewstate-error

and

http://stackoverflow.com/questions/728513/erratic-invalid-viewstate-issue-in-net-application

Mircea Grelus
+1  A: 

One cause for his was fixed with .NET 3.5 SP1. The problem in earlier versions is that the viewstate is rendered at the bottom of the page. If the page is posted back before the entire page is loaded, the viewstate that is being submitted back to the server is incomplete and thus invalid.

I don't know which version of the framework you're using and whether or not you can update. If you can't, you can override the Render-method of your BasePage-class so the viewstate is rendered at the top:

private static string[] aspNetFormElements = new string[] 
    { 
        "__EVENTTARGET",
        "__EVENTARGUMENT",
        "__VIEWSTATE",
        "__EVENTVALIDATION",
        "__VIEWSTATEENCRYPTED",
    };

    protected override void Render(HtmlTextWriter writer)
    {

        StringWriter stringWriter = new StringWriter();

        HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);

        base.Render(htmlWriter);

        string html = stringWriter.ToString();

        int formStart = html.IndexOf("<form");

        int endForm = -1;

        if (formStart >= 0)

            endForm = html.IndexOf(">", formStart);


        if (endForm >= 0)
        {

            StringBuilder viewStateBuilder = new StringBuilder();

            foreach (string element in aspNetFormElements)
            {

                int startPoint = html.IndexOf("<input type=\"hidden\" name=\"" + element + "\"");

                if (startPoint >= 0 && startPoint > endForm)
                {

                    int endPoint = html.IndexOf("/>", startPoint);

                    if (endPoint >= 0)
                    {

                        endPoint += 2;

                        string viewStateInput = html.Substring(startPoint, endPoint - startPoint);

                        html = html.Remove(startPoint, endPoint - startPoint);

                        viewStateBuilder.Append(viewStateInput).Append("\r\n");

                    }

                }

            }


            if (viewStateBuilder.Length > 0)
            {

                viewStateBuilder.Insert(0, "\r\n");

                html = html.Insert(endForm + 1, viewStateBuilder.ToString());

            }

        }

        writer.Write(html);
    }
Stefan
A: 

Disabling TCP Offload Engine (TOE) in Windows 2003/2008 (if enabled) would be a productive first step in determining whether it's related to the network stack or the application or the client:

http://www.onpreinit.com/2009/06/disable-tcp-chimney-to-address-sporadic.html

Nariman