Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use asp.net errors to catch page_error events

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "how to use asp.net error capture page_error events", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in depth, together to study and learn "how to use asp.net error capture page_error events" bar!

ASP.NET provides three main methods for catching and responding to errors when they occur: page_error events, application_error events, and application configuration files (Web.config).

The main function of these three methods is to create custom error reports, and the purpose of creating custom error reports is not only for security reasons, but also to show users friendly error pages and improve the friendliness of the program.

The Page_Error event is illustrated as follows:

This example displays a detailed error message in the browser and is provided for illustrative purposes only. It is dangerous to show the details of the error to the end user of the application (especially the web program). It is more appropriate to display a message to the user informing them that an error has occurred and then record the specific error information in the event log.

This example intentionally throws a null exception in the Page_Load event to test the Page_Error event.

Example: create a web program-create a new page PageEvent.aspx, and then add the following code to PageEvent.aspx:

The copy code is as follows:

Void Page_Load (object sender, System.EventArgs e)

{

/ / throw a null reference exception keleyi.com

Throw (new ArgumentNullException ())

}

Protected void Page_Error (object sender, EventArgs e)

{

Exception objErr = Server.GetLastError () .GetBaseException (); / / get error

String err = "1.error in:" + Request.Url.ToString () + "" +

2.error Message: "+ objErr.Message.ToString () +"+

"3.stack Trace:" + objErr.StackTrace.ToString () + "

Response.Write (err.ToString ()); / / output error message

/ / Response.Redirect ("ErrorPage.htm"); / / you can redirect to a friendly error page

Server.ClearError ()

}

Save, right-click the page-"View in browser" to see the captured custom error message

Thank you for your reading, the above is "how to use asp.net error capture page_error events" content, after the study of this article, I believe you on how to use asp.net error capture page_error events this problem has a deeper understanding, the specific use of the situation also needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report