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 handle global exceptions in MVC

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

Share

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

Editor to share with you how to handle global exceptions in MVC, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

First, the MVC framework's own global exception handling

In MVC, the framework has given us a set of HandleErrorAttribute classes for global exception handling. We can find this line of code in the FilterConfig.cs file in the App_Start folder in MVC

Public static void RegisterGlobalFilters (GlobalFilterCollection filters) {filters.Add (new HandleErrorAttribute ());}

This is to instantiate a HandleErrorAttribute class and put it in the filter. Then we have an Error.cshtml page in our Views > Shared folder, where the type of Model in this page is System.Web.Mvc.HandleErrorInfo, which is already written for me by the MVC framework, and we can use it directly.

In the Error.cshtml page, we can do further processing to display the error message and display the error message according to the requirements. These error messages will be found in some properties in the System.Web.Mvc.HandleErrorInfo class.

For example, the following is Error.cshtml.

We deliberately write an exception in Control:

Public class HomeController: Controller {public ActionResult Index () {string I = "12a"; int j = Convert.ToInt32 (I); return View ();}}

Run it. Let's take a look at the results.

This is the result of the run, and we can see that the System.Web.Mvc.HandleErrorInfo class still has a lot of rich properties that we can use directly.

MVC comes with this set of exception handling default is to handle the error code is 500 series of exceptions, if it is 404, will not use this. However, we can do this through the settings of the Web.config file. Let's see how we handle it.

First, we complete the Error.cshtml page, first add a Control to it, and then we write a View and Control specifically dealing with 404. As follows

Namespace Exception.Controllers {public class SharedController: Controller {/ / GET: Shares public ActionResult Error () {return View ();} public ActionResult NotFondError () {return View ();}

Page:

Then we write the wrong address in the browser address and take a look at the result:

Second, rewrite exception handling in MVC

In development, we often have such a requirement that we need to record and save the exception through a text log, so System.Web.Mvc.HandleErrorInfo, the exception handling method included in MVC, does not have such a function, so we use rewriting to make it have this function. Next, let's take a look at how to rewrite.

First we create a class that inherits from System.Web.Mvc.HandleErrorInfo, and then overrides the virtual method in System.Web.Mvc.HandleErrorInfo: the OnException method.

Public class CustomHandleErrorAttribute: HandleErrorAttribute {public override void OnException (ExceptionContext filterContext) {base.OnException (filterContext); var err = filterContext.Exception.Message;// error content / / = = / / log the error / / =}}

Then, add FilterConfig.cs:

Public class FilterConfig {public static void RegisterGlobalFilters (GlobalFilterCollection filters) {filters.Add (new HandleErrorAttribute ()); filters.Add (new CustomHandleErrorAttribute ());}}

In this way, we can meet our needs.

The above is all the contents of the article "how to handle global exceptions in MVC". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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