How can I coerce an exception message that has bubbled from my business layer into an a开发者_JAVA技巧lert box in javascript?
BTW, I'm using ASP.NET MVC 1.0.
Thanks, Rod.
Make a custom action filter attribute and apply it to the action method. In the override version of OnActionExecuted() you'll have to set the filterContext.Result to some RedirectResult/RedirectRouteResult in order to return anything to the browser as the exception has set it to EmptyResult. So redirect to some error page or controller action which renders html with the javascript for the alert :) Here's the custom ActionFilter Attribute you could start with:
public class ExceptionAlerterFilterAttribute: ActionFilterAttribute
{
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
if(null!= filterContext.Exception && !filterContext.ExceptionHandled)
{
RedirectResult result = new RedirectResult(some_url_you_need_to_set);
filterContext.Result = result;
//yes, you have to set the ExceptionHandled to stop the error bubbling
filterContext.ExceptionHandled = true;
}
base.OnActionExecuted(filterContext);
}
}
Gets a bit messy, but I hope answers your question ?
精彩评论