c# - Exception filter not triggered -


i wanted add exceptionfilter project @ work. made test api project vs2013 on .net 4.5:

  1. a web api action throws custom exception (has filter attribute)

  2. custom exception constructor being build

  3. filter exception catch , handles.

and worked great!

now, went work project, working .net 4.5, created testcontroller file , copy paste code test project file. web api action hit, custom exception constructor called exception filter didn't triggered. why?

this apicontroller, exceptionfilter, customexception , customer class:

using system; using system.collections.generic; using system.io; using system.linq; using system.net; using system.net.http; using system.net.mime; using system.text; using system.web.http; using system.web.http.filters; using system.web.services.description;  namespace web.controllers.webapi {     public class testcontroller : apicontroller     {         public static list<customer> customers;         static testcontroller()         {             customers = new list<customer>()             {                 new customer() {id = 1, name = "person1"},                 new customer() {id = 2, name = "person2"},             };         }      [apiexceptionfilterattribute]     public ihttpactionresult get(int id)     {         if (customers != null)         {             var customer = customers.firstordefault(x => x.id == id);             if (customer == null)             {                 throw new customernotexistsexception(id);             }              return ok(customer);         }          return internalservererror();     } }   public class customer {     public int id { get; set; }     public string name { get; set; } }    public class apiexceptionfilterattribute : exceptionfilterattribute {     public override void onexception(httpactionexecutedcontext actionexecutedcontext)     {         if (actionexecutedcontext.exception customernotexistsexception)         {             throw new httpresponseexception(actionexecutedcontext.request.createerrorresponse(httpstatuscode.internalservererror, ((customernotexistsexception)actionexecutedcontext.exception).message));         }         else         {             //genenral 500 error                 //log exception         }     } }    public class customernotexistsexception : httpresponseexception {     public int customerid { get; set; }     public string message { get; set; }      private const string message = "customer id {0} not found";      public customernotexistsexception(int customerid) : base(new httpresponsemessage())     {         customerid = customerid;         message = string.format(message, customerid);     }      public customernotexistsexception(int customerid, string message) : base(new httpresponsemessage())     {         customerid = customerid;         message = message;     } } } 

this webapiconfig:

public static class webapiconfig {     public static void register(httpconfiguration config)     {         config.routes.maphttproute(             name: "defaultapi",             routetemplate: "api/{controller}/{action}"         );      } } 

and global.asax

public class mvcapplication : httpapplication {      protected void application_start()     {         arearegistration.registerallareas();         webapiconfig.register(globalconfiguration.configuration);         filterconfig.registerglobalfilters(globalfilters.filters);         routeconfig.registerroutes(routetable.routes);         //bundleconfig.registerbundles(bundletable.bundles);      }      void application_error(object sender, eventargs e)     {                                 //some code here     }  } 

update:

just realized when exception filter not triggered status code 200!

very strange since throws exception after all.

the solution worked me change customexception created (customernotexistsexception in code) inherit exception base class , not httpresponseexception.


Comments

Popular posts from this blog

java - Andrioid studio start fail: Fatal error initializing 'null' -

android - Gradle sync Error:Configuration with name 'default' not found -

StringGrid issue in Delphi XE8 firemonkey mobile app -