How to handle Asp.net page errors

Source: Internet
Author: User

1. The first method is to configure in the web. config file.

< System. Web >
< Customerrors defaultredirect = " ~ /Errorpage. aspx "  
Mode
= " Remoteonly " >
</ Customerrors >
</ System. Web >

 

The defaultredirect attribute is used to indicate the oriented page when an unhandled error occurs on the ASPX page; howeverAsp.net uses the redirection mechanism to re-navigate the error page, so that the error information will be lost. That is to say, the exception object obtained using server. getlasterror () is always empty. Although you can prompt the user for an error and provide a link to return the error page, you cannot give the Administrator a good error message.

2. Method 2: process in the application_error method of the Global File

Code

Protected   Void Application_error (Object sender, eventargs E)
{
Exception ex
= Server. getlasterror (). getbaseexception ();

String Errortime = " Occurrence time: " + Datetime. Now. tostring ();
String Erroraddress = " Exception page: " + Request. url. tostring ();
String Errorinfo = " Exception information: " + Ex. message;
String Errorsource = " Error Source: " + Ex. source;
String Errortrace = " Stack information: " + Ex. stacktrace;
Server. clearerror ();

System. Io. streamwriter writer
= Null ;
Try
{
Lock ( This )
{
// Write logs
String Year = Datetime. Now. year. tostring ();
String Month = Datetime. Now. Month. tostring ();
String Day = Datetime. Now. Day. tostring ();
String Path = String . Empty;
String Filename = Datetime. Now. tostring ( " Yyyymmdd " ) + " . Txt " ;
Path
= Server. mappath ( " ~ /Error/ " ) + Year + Month + Day;
If ( ! Directory. exists (PATH ))
{
Directory. createdirectory (PATH );
}
System. Io. fileinfo File
= New Fileinfo (Path + " / " + Filename );
Writer
= New Streamwriter (file. fullname, True ); // If the file is not present, it is created. True indicates appending.
Writer. writeline ( " User IP Address: " + request. userhostaddress);
writer. writeline (errortime);
writer. writeline (erroraddress);
writer. writeline (errorinfo);
writer. writeline (errorsource);
writer. writeline (errortrace);
writer. writeline (
" ------------------------------------------------------- " );

}
}
Finally
{
If (Writer ! = Null )
{
Writer. Close ();
}
}
Server. Transfer (
" ~ /Errorpage. aspx " ); // Hop Go to the page displaying friendly errors

}

Then, some friend prompts are displayed on the errorpage. ASPX page.

3. Method 3: handle in the page_error event

Code

Private   Void Page_load ( Object Sender, system. eventargs E)
{
Throw ( New Argumentnullexception ());
}

Public VoidPage_error (ObjectSender, eventargs E)
{
Exception ex
=Server. getlasterror (). getbaseexception ();

String Errortime = " Occurrence time: " + Datetime. Now. tostring ();
String Erroraddress = " Exception page: " + Request. url. tostring ();
String Errorinfo = " Exception information: " + Ex. message;
String Errorsource = " Error Source: " + Ex. source;
String Errortrace = " Stack information: " + Ex. stacktrace;

Server. clearerror ();

System. Io. streamwriter writer = Null ;
Try
{
Lock ( This )
{
// Write logs
String Year = Datetime. Now. year. tostring ();
String Month = Datetime. Now. Month. tostring ();
String Day = Datetime. Now. Day. tostring ();
String Path = String . Empty;
String Filename = Datetime. Now. tostring ( " Yyyymmdd " ) + " . Txt " ;
Path
= Server. mappath ( " ~ /Error/ " ) + Year + Month + Day;
If ( ! Directory. exists (PATH ))
{
Directory. createdirectory (PATH );
}
System. Io. fileinfo File
= New Fileinfo (Path + " / " + Filename );
Writer
= New Streamwriter (file. fullname, True ); // If the file is not present, it is created. True indicates appending.
Writer. writeline ( " User IP Address: " + request. userhostaddress);
writer. writeline (errortime);
writer. writeline (erroraddress);
writer. writeline (errorinfo);
writer. writeline (errorsource);
writer. writeline (errortrace);
writer. writeline (
" ------------------------------------------- " );

}
}
Finally
{
If (Writer ! = Null )
{
Writer. Close ();
}
}

Server. clearerror ();//Prevent errors from continuing to the application_error event to be processed.
Response. Redirect ("~ /Errorpage. aspx");

}

 

I often use the second method, and then write another method for sending text messages (calling the mobile text message excuse ).ProgramWhen an error occurs, the administrator can receive the program error message.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.