Fall in love with the mvc~ business layer intentionally throws an exception, the global exception captures it and returns it as a format

Source: Internet
Author: User
Tags httpcontext throw exception

Back to Catalog

For the business layer of the program's fatal error, we have been the practice is to directly throw the specified exception, let the program go to the end, this is true, because if a business has a fatal blocking problem, there is no need to go up a layer of return, but then there is a problem, the direct throw exception, means that the server directly 500, the front-end how to display, or if you are the API service, if the front-end return, if it is 500, then directly hung up, haha!

The following is an optimized global exception capture code (non-API) in an MVC environment

    /// <summary>    ///Global Exception Capture/// </summary>     Public classGlobalexceptionfilterattribute:handleerrorattribute { Public Override voidonexception (Exceptioncontext context) {Jsonresult response=NewJsonresult () {Jsonrequestbehavior=Jsonrequestbehavior.allowget}; if(Context. Exception isArgumentException) {                varException =(ArgumentException) context.                Exception; Response. Data=New{StatusCode=System.Net.HttpStatusCode.InternalServerError, ErrorCode=" Geneva", Message=exception.            Message,}; }            Else if(Context. Exception isException) {                varException =(Exception) context.                Exception; Response. Data=New{StatusCode=System.Net.HttpStatusCode.InternalServerError, ErrorCode=" on", Message=exception.            Message,}; }            Else{Response. Data=New{StatusCode=System.Net.HttpStatusCode.InternalServerError, ErrorCode="Geneva", Message="Other Exceptions",                }; } context. Result=response; Context. Exceptionhandled=true; Context.            HttpContext.Response.Clear (); Context. HttpContext.Response.TrySkipIisCustomErrors=true; }    }

If there is a problem with the business layer, throw it directly.

   if 0 )        thrownew ArgumentException ("ID cannot be 0");    if 0 )        thrownew ArgumentException ("ID cannot be negative ");

Then after the page, deliberately let it throw an exception, we look at the results of the page response

In fact, for the server, it is 200, normal return, and for the non-business module, its state is a 500, hehe, this should be clear.

Thank you for reading!

Back to Catalog

Falls in love with the mvc~ business layer deliberately throws an exception, and the global exception captures it and returns it as a format

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.