Use Attribute in ASP.NET Web API unified exception handling

Not all exceptions need to be repeated try-catch process, which can lead to a lot of repetitive code to modify the exception handling mechanism once the system in the follow-up, with the increase in the amount of code modification also become more difficult.

ASP.NET Web API in particular an increase in global exception filter function, such as this there are many filters available to developers choose to implement Aspect Oriented Programming, they replace the repetitive coding on the road this goal, make a lot of contribution, while reducing the difficulty of post-maintenance code to improve readability.

Most try-catch exception handling are usually the same wording (exception log records, returns an error message, etc.), so we can write them in a unified filter, so that API when an exception occurs, even without the use try- catch position nested exceptions and solutions, it is possible to automatically enter a desired exception handling.


Note: This is a global exception handler specifically for the Web API used. If Controller, it has another set of global exception handling mechanism, not only because of the MVC controller return Json string Under normal circumstances, usually involving page View, File File, Json string and so on.


Get Start

Here we have a simple exception handling filter code as an example to illustrate how the global exception handler should take effect:


1. Create a class MyExceptionFilterAttribute, inheritance ExceptionFilterAttribute, System.Web.Mvc.IExceptionFilter and implement an interface method


using Newtonsoft.Json;
using System.Net.Http;
using System.Web.Http.Filters;
using System.Web.Mvc;

WebAPITest.Filters namespace
{
     public class MyExceptionFilterAttribute: ExceptionFilterAttribute, System.Web.Mvc.IExceptionFilter
     {
         // MVC filter inherit this interface requirements must be allowed to register, but we can inherit achieve but do not write it.
         void the OnException public (ExceptionContext filterContext)
         {
             the throw the NotImplementedException new new ();
         }

        // API allows real global exception filter takes effect when this guy is an exception:
         public void the override OnException (HttpActionExecutedContext actionExecutedContext)
         {
             base.OnException (actionExecutedContext);

            actionExecutedContext.Response = new HttpResponseMessage() { Content = new StringContent("出现异常") };
             return;

        }

}



2. To this end the global exception handler to register:

Open the folder FilterConfig.cs under App_Start registered as a filter, thus in order to make the filter effect.


Special Note: Once registered globally, all for exception occurs, the filter will take effect.


public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
     filters.Add(new MyExceptionFilterAttribute());
}



Use as a feature tag

Of course, in addition to the above-described MyExceptionFilterAttribute registered in the global exception filter outside, may be used alone for certain methods in the API :( Note that the method itself is "try", the filter is a "catch". Is no longer required in vivo again try-catch)

using System.Net.Http;
using System.Web.Http;
using WebAPITest.Filters;
using WebAPITest.Models;

WebAPITest.Controllers namespace
{
     // You can also filter tag play in this place, which represents all interfaces in the API classes are accepted [MyExceptionFilter] took exception handling.
     TestController class public: ApiController
     {
         [MyExceptionFilter] // feature tag to play here, then UserException unusual method of treatment MyExceptionFilterAttribute will trigger an exception.
         HttpResponseMessage a UserException public ()
         {
             the throw new new a UserException ( "user exception");
         }

        [MyExceptionFilter] // above, this feature will help SystemException label deal with an exception.
         HttpResponseMessage SystemException public ()
         {
             the throw new new Exception ();
         }

    }
}



Priority issues and global registration of labels

A plurality of characteristic tags, global register may be separately on a method name, a mixture of the class name, then if a global tag is registered, another tag alone was hit on the method name class name, which is a final processing or an approach would be triggered?

In fact, these guys priority and priority css cascading style sheets similar.

That is: If we registered a global filter A, but at the top playing another B ApiController class, but also to play a third exception handler C on the Action method, then the default will execute the closest Action C processor, that is to say: ActionFilter> ClassFilter> GlobalFilter.

Also, if needed repeatedly into full force, namely: a global one, the Controller class once, Action once, the need to add it to the top of the filter when a label [AttributeUsage (AttributeTargets.All, AllowMultiple = true)], then the trigger when the abnormality they will be able to support multiple executions.

Of course, the real exception handling also involves a lot of complex content, this is just an overview. For example return values ​​need to be normalized (contains status code, message, response content flow, etc.), call exception handling tool transmits abnormality information to a server recording, the recording request parameter, leading to abnormal differences in treatment methods for GET / POST request.

But we are here to provide an idea is to let you know, streamline the code, improve development efficiency can actually have more ways and possibilities. And do not have to use the same way, when you feel that certain steps have been repeated, it should have been early developers propose a solution, and you need to find them.

Guess you like

Origin blog.51cto.com/14360220/2416919