This article is a mirror article of machine translation, please click here to jump to the original article.

View: 30889|Reply: 0

[ASP.NET] ASP.NET add a global anomaly filter to MVC

[Copy link]
Posted on 6/4/2021 5:50:35 PM | | |
It's not scary for a website to be unusual, but the scary thing is that there is no logging.

Introduction to Global Exception Capture in MVC:

Global exception capture is a mechanism that Microsoft comes with in VS. In Asp.Net MVC, an exception filter HandleError feature is provided by default, which can be used to capture and handle exceptions thrown by controllers and operations, it will automatically detect errors running in the program, and get information about the errors. In the Asp.Net MVC, as long as the website throws an exception, it will trigger the Application_Error event in Global.asax, this method is used to catch exceptions, in this code we can add our business code (generally to record error logs and exception handling).

In MVC, you can also use the HandleErrorAttribute property tag to catch exceptions, which is mainly used for specific operation methods, and we rarely use it in writing projects. I won't introduce it to you here, if you want to know, Baidu is fine.

Introduction to custom exception capture in MVC:

In Asp.Net, not only can we use global exception capture, but Microsoft also provides us with a custom exception capture to meet our needs. Of course, in the custom exception capture, we still use this feature of HandleErrorAttribute provided by VS to implement it, and rewrite the OnException() method of HandleErrorAttribute in our own defined class to implement our business.

Review:

asp.net MVC global exception handling and exception logs
https://www.itsvse.com/thread-3250-1-1.html

.net/c# Exception handling when the global is not captured
https://www.itsvse.com/thread-3480-1-1.html
Code:







Previous:Prevent Host header attacks on the web server
Next:ASP.NET Core is deployed as a Windows service
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com