Error Handling

A

A.M

Hi,

I know if I put this settings in web.config file, all unhandled exceptios
will be redirected to "genericerror.aspx":

<configuration>
<system.web>
<customErrors defaultRedirect="genericerror.aspx" mode="RemoteOnly" />
</system.web>
</configuration>


Inside "genericerror.aspx", Is there any way to find what was the unhandled
exception?

Thanks,
Ali
 
S

Steven Cheng[MSFT]

Hi Ali,

From your description, you used an aspx page to perform as the custom error
page which is set via the <customErrors defaultRedirect='..'> element in
the web.config. And now you'd like to get the unhandled error/exception 's
detailed info in the page via code, yes?

Generally, the ASP.NET web application's unhandled exceptions can be
captured in the Global class's Application_Error event.
Here are some references on this:
#Code: Handling Application-Level Errors (Visual Basic)
http://msdn.microsoft.com/library/en-us/dv_vbcode/html/vbtskcodeexamplehandl
ingapplicationlevelerror.asp?frame=true

#Rich Custom Error Handling with ASP.NET
http://msdn.microsoft.com/library/en-us/dnaspp/html/customerrors.asp?frame=t
rue#customerrors_topic7

And since by default, after the Application_Error event, the ASP.NET
runtime will clear the current context( last error ) and restart a new
session(stop the old one), this will cause us unable to retrieve the
exception info via "Server.GetLastError()" in the custom error page. If we
want to persist the error info or the current context, we need to use
"Server.Transfer" to manually redirect the user to our custom error page,
For example:
#in Global object's Appliation_Error event:
protected void Application_Error(Object sender, EventArgs e)
{
Server.Transfer("CustomErrorPage.aspx");
}

Then, in the "CustomErrorPage.aspx" 's page load event, we can use the
below code to retrieve the last unhandled error occured in the applicatino:
private void Page_Load(object sender, System.EventArgs e)
{
Exception ex = Server.GetLastError();
// show it on the page
}

In addition, here is another former post discussing on the same problem,
you may refer to if via the below weblink in google:

#Subject: Re: How To Create Custom Aspx Error Reporting Page
http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&threadm=UGnEEkB%
24DHA.404%40cpmsftngxa06.phx.gbl&rnum=2&prev=/groups%3Fq%3Dasp.net%2Bredirec
t%2Berror%2Bsteven%2Bcheng%26ie%3DUTF-8%26oe%3DUTF-8%26hl%3Den

Hope these help.


Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top