Introduction to ASP. NET custom error pages

Source: Internet
Author: User

Aside from try catch, ASP. NET provides four error handling mechanisms:Page_error, errorpage, application_error, <customerrors>Four types. If you add try catch, five types of variables can be understood as: a local variable --- try catch, two types of page-level variables: page_error, errorpage, and two global variables: application_error, customerrors: here we will explain their execution sequence in advance, from high to low:Page_error> errorpage> application_error> <customerrors>The following describes how to use these four operations in a detailed order.

First:Page_errorWhen we look at this word, we will probably understand what it means, that is, page-level processing.ProgramA page error occurs. A's page_error processes a's error. B's page_error processes B's error. You can try catch to understand it, this is a little larger than the try catch range. A method contains try catch. If this method fails, it will be processed in catch. If a page error occurs, it will be processed in page_error.

 

Usage: add this method to the page.

Protected void page_load (Object sender, eventargs E)

{

// Operations after an error, such as writing logs, output the expected error message to the user

}

Advantage: flexible processing. You can simply add pages with frequent errors.

Disadvantage: it is a page-level variable. More often, we don't know which pages will encounter errors. Therefore, if you use this processing method, you need to write all the pages, which is troublesome.

 

Second:ErrorpageIn fact, errorpage and page_error are very similar. We can think of them as front-end JS processing and page-level processing programs, but they are not recommended here. It may be due to habits and I personally feel ASP. the most important aspect of net over ASP isCodeIn this case, why do I have to write things on the background at the front end? Haha ,.. Of course, this is also a personal habit. A lot of things must be written at the front-end. That's no way to do it. Let's take a look at his usage.

 

Usage: add this method to the page.

< Script Language = " C # " Runat = " Server " >
Protected Void Page_load ( Object Sender, eventargs E)

{
This. Errorpage="Errorpage.htm";
}
</Script>

Advantage: see page_error

Disadvantage: see page_error

 

Third:Application_errorSeeApplication_error, FamiliarASP. NETThe person knows at a glance that it belongsGlobal. asaxIt is a global processing mechanism. We should not only ask,Application_errorAndCustomerrorsThey are all global variables. What are their differences? The inconsistency in the mechanism will not be mentioned here. The usage is different,CustomerrorsYou can only jump to the page you want, but cannot record the error information. It will automatically process it,Application_errorReceive and handle these error messages

 

Usage: in global. asaxProcessing,Application_errorMethodGlobalWill automatically generate

Protected Void Application_error ( Object Sender, eventargs E)

{

Exception ex=This. Context. server. getlasterror ();

If(EX! =Null)

{

This. Context. server. Transfer ("/Error. aspx");

//Logs can be written here, or logs can be written by error, and a friendly interface is displayed to the user.

This. Context. response. Clear ();

}

}

Advantage: global variables are written once here. If errors occur on all pages, the error page is automatically redirected, saving time and effort, and relevant errors can be recorded.

Disadvantage: global variables are clearly identified, and some performance needs to be consumed.

Fourth:CustomerrorsIn fact, customerrorsYesWeb. configAfter this configuration item is configured, the page will automatically jump to the corresponding error page after an error occurs, which is believed to be frequently used. Here is a brief introduction.

Usage:

< System. Web >
< Customerrors Mode = "On" Defaultredirect = "Genericerrorpage.htm" >
< Error Statuscode = "403" Redirect = "Error403.htm" />
< Error Statuscode = "404" Redirect = "Error404.htm" />
</ Customerrors >
</ System. Web >

Advantage: For those page connections that do not exist or program errors, the user is prompted with a friendly, this is a lot

Disadvantage: Unable to record specific error information

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.