Support for error handling in ASP. NET

Source: Internet
Author: User
ASP. NET supports ASP. NET application Program It is very easy to add custom error information. First, write your own web page, which can be any type of Files:. htm,. aspx,. asp, and so on. Modify the configuration information in the config. Web file of the application to point it to the file. For example, the following configuration information indicates that the browser should be redirected to "errorpage. ASPX page: <configuration> <customerrors mode = "remoteonly" defaultredirect = "errorpage. the "defaultredirect" attribute in the aspx "/> </configuration> <customerrors> tag defines the" default "page to which the user will be redirected when an error occurs. Alternatively, you can also Code Status, redirect to other pages to overwrite this default value. For example, redirect to a special "file not found" error page, "illegal access" error page, or "server conflict" error page. For example, the following configuration information overwrites three specific HTTP status codes. All other errors are returned to a default page: <customerrors defaultredirect = "http: // anotherhost/error. aspx "mode =" remoteonly "> <Error statuscode = "500" Redirect = "http:/anotherhost/pages/callsupport.html"/> <Error statuscode = "404" Redirect = "http:/anotherhost/pages/adminmessage.html"/> <Error statuscode = "403" Redirect = "http:/anotherhost/pages/noaccess.html"/> </customerrors> we have encountered one thing on the custom error page, that is, although they are very useful for completed situations, they are very difficult to deal with during the development process. It is because you expect that there will be bugs in the development process, and when you find it, you really want to see the actual error information tracking. To solve this problem, the <customerrors> tag supports a "Mode" attribute with three values: "on", which means that a custom error page is always issued; "off ": this means that the custom error page is never sent (you always see the original error message); "remoteonly ": this means that the custom error page is issued only when the remote browser clicks the site (the developer who clicks the site on the actual machine sees the detailed error information ). Installing applications helps administrators track errors. Although displaying custom error messages to customers is a good thing, you may still want to make it happen on the site, developers and administrators can easily discover it in real time and identify the problem, URL, and exception information. To solve this problem, ASP. NET introduces a new "Application Layer" event that can be processed in the global. asax file: "application_error ". In the process of processing a Web request, this method is called when an unprocessed exception occurs. Developers can obtain special information about the request, such as the URL of the page to be displayed, the query string variable, the user agent, and the cookie value, and the information of the actual exception object that encapsulates the error information. Then you can continue and run any logic that they want to track and notify administrators and developers of problems. This may include using system. diagnostic APIs writes information to the net Event Log and uses system. web. util SMTP Email APIs sends an email to the Administrator and writes information to a database. For example, the following global. the asax file demonstrates how to write error information to a custom NT Event Log "mycustomlog", including the page url and exception stack record: <% @ import namespace = "system. diagnostics "%> <script language =" VB "runat = Server> sub application_error (sender as object, e as eventargs) 'obtain the URL of the requestdim pageurl as string = request. path 'obtain the exception object for the errordim errorinfo as exception = server. getlasterror () 'construct error message to write NT Event logdim message as string = "url" & pageurlmessage = message & "error:" message = message & errorinfo. tostring 'nt Event Log name to write message todim LOGNAME as string = "mycustomlog" 'create Event Log if it doesn' t existif (not EventLog. sourceexists (LOGNAME) theneventlog. createeventsource (LOGNAME, LOGNAME) end if 'fire off to event logdim log as new eventloglog. source = lognamelog. writ Eentry (message, eventlogentrytype. error) end sub </SCRIPT> if you want to see how everything actually works, try to create a new IIS application vroot on the machine, then copy the sample code above to a new "Global. in the asax file, this file is created in the application root directory. Then, copy/paste the following code snippet to "blowup. in the aspx file: <HTML> <body> <% dim xx. blowup () %> </body>
Related Article

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.