I am new to aspx webforms.
I want to catch a specific Exception in my Web Application – Validation of viewstate MAC failed.
I tried this (in Global.asax.cs):
protected void Application_Error(object sender, EventArgs e)
{
HttpException lastErrWrapper = Server.GetLastError() as HttpException;
if ((uint)lastErrWrapper.ErrorCode == 0x80004005)
{
// do something
}
}
The problem is that it catches all unhandled HttpExceptions.
What is the best way to achieve this ?
edit:
While checking this issue further I found that the inner exception is a ViewStateException, but it doesn’t seem to have a specific “errorCode” attribute
Thanks
This should do it
The HttpException is designed to make all the HTTP/web related stuff be catchable by one handler, so you need to dig in and look at the original exception. ViewStateException might catch a couple of other View State-related errors, but that’s probably OK.