仅捕获特定的HttpException

Bas*_*sal 5 c# asp.net webforms

我是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
  }         
}
Run Code Online (Sandbox Code Playgroud)

问题是它捕获了所有未处理的HttpExceptions.

实现这一目标的最佳方法是什么?


编辑:

在进一步检查此问题时,我发现内部异常是a ViewStateException,但它似乎没有特定的"errorCode"属性

谢谢

MNG*_*inn 5

这应该做到这一点

if ((lastErrWrapper != null) && (lastErrWrapper.InnerException != null) 
  && (lastErrWrapper.InnerException is ViewStateException)
{
}
Run Code Online (Sandbox Code Playgroud)

HttpException旨在使所有与HTTP/Web相关的东西都可以由一个处理程序捕获,因此您需要深入研究并查看原始异常.ViewStateException可能会捕获其他一些与View State相关的错误,但这可能没问题.