我是aspx webforms的新手。
我想在我的Web应用程序中捕获一个特定的异常 - Validation of viewstate MAC failed
我试过这个(在Global.asax.cs中):
protected void Application_Error(object sender, EventArgs e)
{
HttpException lastErrWrapper = Server.GetLastError() as HttpException;
if ((uint)lastErrWrapper.ErrorCode == 0x80004005)
{
// do something
}
}
问题是它捕获了所有未处理的HttpExceptions。
实现这一目标的最佳方法是什么?
编辑:
在进一步检查此问题时,我发现内部异常是ViewStateException
,但它似乎没有特定的“errorCode”属性
谢谢
答案 0 :(得分:5)
这应该这样做
if ((lastErrWrapper != null) && (lastErrWrapper.InnerException != null)
&& (lastErrWrapper.InnerException is ViewStateException)
{
}
HttpException旨在使所有与HTTP / web相关的东西都可以由一个处理程序捕获,因此您需要深入研究并查看原始异常。 ViewStateException可能会捕获其他一些与View State相关的错误,但这可能没问题。
答案 1 :(得分:1)
以下是我们为帮助解决global.asax中的ViewState错误而实施的内容:
Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
Dim context As HttpContext = HttpContext.Current
Dim exception As Exception = Server.GetLastError
'custom exception handling:
If Not IsNothing(exception) Then
If Not IsNothing(exception.InnerException) Then
'ViewState Exception:
If exception.InnerException.GetType = GetType(ViewStateException) Then
'The state information is invalid for this page and might be corrupted.
'Caused by VIEWSTATE|VIEWSTATEENCRYPTED|EVENTVALIDATION hidden fields being malformed
' + could be page is submitted before being fully loaded
' + hidden fields have been malformed by proxies or user tampering
' + hidden fields have been trunkated by mobile devices
' + remotly loaded content into the page using ajax causes the hidden fields to be overridden with incorrect values (when a user navigates back to a cached page)
'Remedy: reload the request page to replenish the viewstate:
Server.ClearError()
Response.Clear()
Response.Redirect(context.Request.Url.ToString, False)
Exit Sub
End If
End If
End If
End Sub