Related handling of MVC exception cases

Source: Internet
Author: User
Tags try catch
This article is mainly for you to introduce the MVC exception processing related information, with a certain reference value, interested in small partners can refer to

In the daily development, we will go to catch a lot of anomalies, to deal with, usually our method is to be in need of exception processing place with a try catch block, but, if there is a lot of exception processing, then, will frequently write the try catch block, for our natural ' lazy ' Programmer, always want to find a shortcut. So, there will be global exception handling, so today we'll look at how to do global exception handling in MVC.

First, the MVC framework's own global exception handling

In MVC, the framework has given us a set of attribute class Handleerrorattribute classes for global exception handling. We can find such a 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 the instantiation of a Handleerrorattribute class, which is placed in the filter. And then our views>. There is a error.cshtml page in the shared folder, where the model type of the page is System.Web.Mvc.HandleErrorInfo, and this is what the MVC framework has written for me, which we can use directly.

In the Error.cshtml page, we can do further processing to display the error message and to display the error message on demand. These error messages are found in some of the properties in the System.Web.Mvc.HandleErrorInfo class.

For example: the error.cshtml.

In control, we deliberately write an exception:


public class Homecontroller:controller  {public    actionresult Index ()    {      String i = "12a";      Int J = Convert.ToInt32 (i);      return View ();    }  }

Run, let's look at the results.

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.