About MVC Exception Handling and mvc Exception Handling

Source: Internet
Author: User
Tags try catch

About MVC Exception Handling and mvc Exception Handling

In daily development, we capture many exceptions for processing. Generally, we add a try catch block where Exception Processing is required. However, if you need to handle many exceptions, you will frequently write try catch blocks. For programmers who are naturally 'Lazy ', they always want to find a shortcut. So there will be global Exception Processing. Today, we will look at how to handle global exceptions in MVC.

I. MVC Framework's own global Exception Handling

In MVC, the framework has provided 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 of MVC.

1         public static void RegisterGlobalFilters(GlobalFilterCollection filters)2         {3             filters.Add(new HandleErrorAttribute());4         }

 

This is to instantiate a HandleErrorAttribute class and put it in the filter. Then there is an Error in our Views> Shared folder. cshtml page, where the Model type in this page is System. web. mvc. handleErrorInfo, which has been written by the MVC framework, can be used directly.

On the Error. cshtml page, we can perform further processing to display the Error information and display the Error information as needed. These error messages are found in some attributes of the System. Web. Mvc. HandleErrorInfo class.

For example, Error. cshtml.

In Control, we intentionally write an exception:

1     public class HomeController : Controller2     {3         public ActionResult Index()4         {5             string i = "12a";6             int j = Convert.ToInt32(i);7             return View();8         }9     }

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

The above is the running result. We can see that the System. Web. Mvc. HandleErrorInfo class still has many rich attributes, which can be used directly.

The default Exception Handling Method of MVC is to handle 500 series exceptions. If it is 404, it will not be used. However, we can use the settings of the Web. config file for processing. Let's see how we handle it.

First, we need to complete the Error. cshtml page, add a Control to it, and then write a View and Control specially designed to process 404. As follows:

 1 namespace Exception.Controllers 2 { 3     public class SharedController : Controller 4     { 5         // GET: Shares 6         public ActionResult Error() 7         { 8             return View(); 9         }10 11         public ActionResult NotFondError()12         {13             return View();14         }15     }16 }

Page:

Then, write an incorrect address in the browser address and check the result:

Ii. Rewrite Exception Handling in MVC

During development, we often have such a requirement. We need to record and save exceptions through text logs, so the exception handling method System provided by MVC is. web. mvc. handleErrorInfo does not have such a function, so we can rewrite it to provide this function. Next, let's take a look at how to rewrite it.

First, we create a class to Inherit System. Web. Mvc. HandleErrorInfo, and then rewrite the virtual method: OnException in System. Web. Mvc. HandleErrorInfo.

1 public class CustomHandleErrorAttribute: HandleErrorAttribute 2 {3 public override void OnException (ExceptionContext filterContext) 4 {5 base. onException (filterContext); 6 var err = filterContext. exception. message; // error content 7 // the error is recorded in the log 9 // ============================== 10} 11}

Then, add FilterConfig. cs:

1     public class FilterConfig2     {3         public static void RegisterGlobalFilters(GlobalFilterCollection filters)4         {5             filters.Add(new HandleErrorAttribute());6             filters.Add(new CustomHandleErrorAttribute());7         }8     }

In this way, we can fulfill our needs.

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.