In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the knowledge of "how to handle exceptions in ASP.NET applications". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Occasionally, some exceptions are not handled due to carelessness when we write the program. If the exception details are prompted to the user, it will bring unsafe factors, and if the details are not prompted, it will cause trouble for the user to report the exception. Although you can configure customErrors mode so that only the administrator can view errors, it may take a long time to discover the problem. Through global exception handling, you can record the exception immediately when the exception occurs, or send an e-mail report directly to the administrator to find and handle the exception as quickly as possible.
The main way to add global exception handling to an ASP.NET application is to add the handling of HttpApplication's Error event. This is similar to the URL rewriting method in the BeginRequest event. First build a class that inherits System.Web.IHttpModule. Then add the Init method and the ShowError method.
Namespace Snowdream {public class HttpModule: System.Web.IHttpModule {public void Init (System.Web.HttpApplication context) {context.Error + = new EventHandler (ShowError);} private void ShowError (object sender, EventArgs e) {/ / exception handling here}
Then you need to configure web.config 's httpModules for it to take effect.
My favorite practice now is to record the details of the exception in the database, then return a number, redirect the page to the error prompt page written by myself, and prompt the user for this number. if the user only needs to provide the error number, the administrator can find the error details and modify them in the background.
Here are two problems I encountered when doing global exception handling and my solutions
* the exception hResult is obtained. Since it is private and cannot be accessed directly, the solution is to obtain the exception through (int) System.Runtime.InteropServices.Marshal.GetHRForException (exception).
The second problem is that many errors are recorded that are not caused by the ASP.NET application, such as the user typing an incorrect URL, and the exception handling system records that "the file does not exist." , and these error records not only do not really reflect the exceptions of the ASP.NET application, but will cause more trouble for the administrator. My solution is to filter these HttpException by judging the if (exception is System.Web.HttpException).
This is the end of "how to handle exceptions in ASP.NET applications". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.