c# - Is there a good way to handle exceptions in MVC ChildActions -


i seem doing lot of exception swallowing child actions.

    [childactiononly]     [outputcache(duration = 1200, varybyparam = "key;param")]     public actionresult childpart(int key, string param)     {         try         {             var model = doriskyexceptionpronething(key, param)             return view("_childpart", model);         }         catch (exception ex)         {             // log elmah using helper method             errorlog.logerror(ex, "child action error ");              // return pretty bit of html avoid whitescreen of death on client             return view("_childactiononlyerror");         }     } 

i feel i'm cutting , pasting heaps of code, , each cut paste know kitten being drowned in angels tears.

is there better way manage exceptions in child actions allow rest of screen render appropriately?

you create customhandleerror attribute based on mvc's handleerror attribute, override onexception method, logging , possibly return custom view.

public override void onexception(exceptioncontext filtercontext) {     // log elmah using helper method     errorlog.logerror(filtercontext.exception, "oh no!");      var controllername = (string)filtercontext.routedata.values["controller"];     var actionname = (string)filtercontext.routedata.values["action"];      if (!filtercontext.httpcontext.iscustomerrorenabled)     {         filtercontext.exceptionhandled = true;         filtercontext.httpcontext.response.clear();         filtercontext.httpcontext.response.statuscode = 500;         filtercontext.httpcontext.response.tryskipiiscustomerrors = true;          var model = new handleerrorinfo(filtercontext.exception, controllername, actionname);         filtercontext.result = new viewresult         {             viewname = "_childactiononlyerror",             mastername = master,             viewdata = new viewdatadictionary(model),             tempdata = filtercontext.controller.tempdata         };         return;     } } 

then decorate controllers and/or actions want enable logic so:

[childactiononly] [outputcache(duration = 1200, varybyparam = "key;param")] [customhandleerror] public actionresult childpart(int key, string param) {     var model = doriskyexceptionpronething(key, param)     return view("_childpart", model); } 

Comments

Popular posts from this blog

java - How to specify maven bin in eclipse maven plugin? -

single sign on - Logging into Plone site with credentials passed through HTTP -

php - Why does AJAX not process login form? -