开发者

ASP.Net Invalid Viewstate error

开发者 https://www.devze.com 2022-12-09 11:53 出处:网络
I am randomly encountering the following error: Message: Invalid viewstate. Client IP: xx.xxx.xxx.xx Port: 2324

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开发者_Go百科.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?*


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);
    }


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:

Should I ignore the occasional Invalid viewstate error?

and

Erratic Invalid Viewstate issue in a .NET application


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.


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


Another often less mentioned cause of "Invalid View State" errors showing up in your error log / event viewer is because your running servers behind a content switch that are not running the latest version of .NET updates.

Its usually best practice to keep all your servers in your farm / behind your content switch as identical as possible.

0

精彩评论

暂无评论...
验证码 换一张
取 消