Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I got it to work!</p> <p>For some odd reason, all I needed to do was to pass on the <code>ViewData</code> to a new <code>ResultView</code>. </p> <p>Here's the complete code:</p> <pre><code>public class ErrorLoggingAttribute : ActionFilterAttribute, IExceptionFilter { private String _controllerName, _actionName; private Boolean _redirectToGenericView = false; public ErrorLoggingAttribute() { } public ErrorLoggingAttribute(String actionName, String controllerName) { _controllerName = controllerName; _actionName = actionName; _redirectToGenericView = true; } void IExceptionFilter.OnException(ExceptionContext ec) { // log error if (_redirectToGenericView) { ec.Result = new RedirectToRouteResult(new RouteValueDictionary { {"controller", _controllerName}, {"action", _actionName} }); } else { ec.Result = new ViewResult { ViewName = ((RouteData) ec.RouteData).Values["action"].ToString(), TempData = ec.Controller.TempData, ViewData = ec.Controller.ViewData }; } ec.ExceptionHandled = true; } } </code></pre> <p><br /></p> <h2>Usage</h2> <p><br /> Here's how you would use the attribute on a controller-action, to redirect to the same view (<em>with</em> it's associated model) to enable standard validation-helpers to kick in, when an exception occurs:</p> <pre><code>[ErrorLogging] [AcceptVerbs(HttpVerbs.Post)] public ActionResult Create(Models.Entities.Message message) { var dc = new Models.DataContext(); dc.Messages.InsertOnSubmit(message); dc.SubmitChanges(); return RedirectToAction("List", new { id = message.MessageId }); } </code></pre> <p>And here's how you would use the attribute, to redirect to a generic view, when an exception occurs:</p> <pre><code>[ErrorLogging("ControllerName", "ViewName")] [AcceptVerbs(HttpVerbs.Post)] public ActionResult Create(Models.Entities.Message message) </code></pre> <p><br /> This is a <em>complete</em> separation of logic. Nothing in the controller but the very basics.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload