使用 ASP.NET 和 C# 的“Base-64 字符串中的无效字符”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1051013/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 07:02:33  来源:igfitidea点击:

"Invalid Character in Base-64 String" using ASP.NET and C#

c#asp.net-ajax

提问by Eduardo Mello

I'm having this problem on posting a page. The page have a jquery ajax load called by the onchange of a dropdownlist, if I disable the onchange, the post works.

我在发布页面时遇到了这个问题。该页面有一个由下拉列表的 onchange 调用的 jquery ajax 加载,如果我禁用 onchange,则该帖子有效。

"The state information is invalid for this page and might be corrupted"

“此页面的状态信息无效,可能已损坏”

[FormatException: Invalid character in a Base-64 string.]
   System.Convert.FromBase64String(String s) +0
   System.Web.UI.ObjectStateFormatter.Deserialize(String inputString) +72
   System.Web.UI.ObjectStateFormatter.System.Web.UI.IStateFormatter.Deserialize(String serializedState) +4
   System.Web.UI.Util.DeserializeWithAssert(IStateFormatter formatter, String serializedState) +37
   System.Web.UI.HiddenFieldPageStatePersister.Load() +113

[ViewStateException: invalid Viewstate. 
         Client IP: 127.0.0.1
         Port: 
         User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)
         ViewState: /wEPDwULLTE1NjAwNjAwNDMPZBYCZg9kFgRmD2QWAgIOD2QWCAIBDxYCHgRUZXh0BSpTcGVyaW5kZSBJbcOzdmVpcyAmcnNhcXVvOyBQw6FnaW5hIEluaWNpYWxkAgMPFgIfAAUOdmVuZGFyLCBhbHVnYXJkAgUPFgIfAGVkAgcPFgIfAAUaaHR0cDovL3d3dy5zcGVyaW5kZS5jb20uYnJkAgEPZBYQAgMPEGQQFQEPVGlwbyBkZSBJbcOzdmVsFQEAFCsDAWdkZAIEDxBkEBUBBkNpZGFkZRUBABQrAwFnZGQCBg8QZA8WAWYWARAFD1RpcG8gZGUgSW3Ds3ZlbGVnZGQCBw8QD2QWAh4Ib25jaGFuZ2UFGSQuY2hhbmdlQ2l0eSh0aGlzLnZhbHVlKTsPFgFmFgEQBQZDaWRhZGVlZ2RkAggPZBYQAgEPFgIfAAUMTE9GVCBWSVNDQVlBZAIDDxYCHwAFKUNhc2EgMyBkb3JtaXTDs3Jpb3Mgbm8gYmFpcnJvIEJlbGEgVmlzdGEuZAIHDxYCHwAFHjxzdHJvbmc+UiQgMjUwLjAwMCwwMDwvc3Ryb25nPmQCCQ8WAh8ABRUxNTYwLjQ0IG08c3VwPjI8L3N1cD5kAgsPFgIfAAUJMyBlIDQgIEQuZAINDw8WBh8ABRBFeGNsdWlyIGRhIGxpc3RhHgdUb29sVGlwBRBFeGNsdWlyIGRhIGxpc3RhHgtOYXZpZ2F0ZVVybAUUI3JlbW92ZUxpbmsgMjEwMDM3NzlkZAIPDw8WBh8DBUB+L3ZlbmRhL2xhbmNhbWVudG...]

[HttpException (0x80004005): As informa??es sobre estado s?o inválidas para esta página e podem estar corrompidas.]
   System.Web.UI.ViewStateException.ThrowError(Exception inner, String persistedState, String errorPageMessage, Boolean macValidationError) +106
   System.Web.UI.ViewStateException.ThrowViewStateError(Exception inner, String persistedState) +14
   System.Web.UI.HiddenFieldPageStatePersister.Load() +217
   System.Web.UI.Page.LoadPageStateFromPersistenceMedium() +105
   System.Web.UI.Page.LoadAllState() +43
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +6785
   System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +242
   System.Web.UI.Page.ProcessRequest() +80
   System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context) +21
   System.Web.UI.Page.ProcessRequest(HttpContext context) +49
   ASP.content_search_default_aspx.ProcessRequest(HttpContext context) in c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\web-site-2009\e4bfc9d6\d5d6c855\App_Web_zcb1qfmu.0.cs:0
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +181
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +75

what could be the reason for that? Anyone seen it before?

那可能是什么原因?有人见过吗?

Thank you

谢谢

EDIT:

编辑:

So, I've found out why this is happenning. At the end of Page_Load, I write a javascript line that calls a jquery function that loads new fields into a div. So, as I see, this is causing to create a inconsistence on the viewstate. Disabling ViewState is not a option for this case. It works on FF, and it bugs on IE. Anyone?

所以,我发现了为什么会发生这种情况。在 Page_Load 的末尾,我编写了一个 javascript 行,它调用一个 jquery 函数,将新字段加载到一个 div 中。所以,正如我所看到的,这导致了视图状态的不一致。在这种情况下,禁用 ViewState 不是一个选项。它适用于 FF,它在 IE 上出错。任何人?

Thank you

谢谢

采纳答案by Mike

Just ran into this. Remove the form tag from the page you are loading via jquery.

刚碰到这个。从您通过 jquery 加载的页面中删除表单标记。

回答by Martin

Had the same problem, the reason why I got the error was because I sent a complete form as string into a javascript method, which then posted the form.

有同样的问题,我收到错误的原因是因为我将一个完整的表单作为字符串发送到一个 javascript 方法中,然后该方法发布了该表单。

That was a bit heavy, and my fix was to send only the form data as serialized json, and then create and post the form through javascript, like in this post.

这有点沉重,我的解决方法是仅将表单数据作为序列化的 json 发送,然后通过 javascript 创建并发布表单,就像在这篇文章中一样。

http://weblogs.asp.net/hajan/archive/2011/03/16/posting-from-asp-net-webforms-page-to-another-url.aspx

http://weblogs.asp.net/hajan/archive/2011/03/16/posting-from-asp-net-webforms-page-to-another-url.aspx

回答by Mike Darwish

invalid view state don't have any value for your logger or for users or for your website to avoid this error try to add the following In Global.ascx:

无效的视图状态对您的记录器或用户或您的网站没有任何价值,以避免此错误尝试在Global.ascx 中添加以下内容:

void Application_Error(object sender, EventArgs e)
    {          
                if (ex is HttpException && ex.InnerException is ViewStateException)
                {
                    Response.Redirect(Request.Url.AbsoluteUri);
                    return;
                }
    }

for more info check the following link:

有关更多信息,请查看以下链接:

https://www.karpach.com/viewstateexception-invalid-viewstate.htm

https://www.karpach.com/viewstateexception-invalid-viewstate.htm