asp.net - Can I run every ContentResult in a Controller through a universal try/catch elegantly? -


i assuming there elegant way this, not know is. in application working on, have lot of contentresults return xml. every action return xml runs through exact same try/catch block , repeating myself on , on again - here example of talking about:

public contentresult someaction() {     try     {         //some stuff here     }     catch(exception ex)      {          handleerrormethod(ex);     }     return this.content(someobject.toxmlstring(), contentreturntype); }         

this happens 3-4 times in controller figure there way can either paint attribute, or run sort of method in global.asax or prevent repeating myself on , on - not mention code changes in future. have no idea (i suppose why asking); thank you!

i have had problem. override controller.onexception method handle own way, including logging , either redirecting user error page or displaying javascrpt error depending on request.

you can use handleerror attribute working on actions need it.

protected override void onexception(exceptioncontext filtercontext) {  if (filtercontext == null)         return;  var ex = filtercontext.exception; //your exception //now put rest of code in handle error method.  }  [handleerror] public contentresult someaction() {    //some stuff here    return this.content(someobject.toxmlstring(), contentreturntype); }    

Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

visual c++ - Using relative values in array sorting ( asm ) -