简体   繁体   中英

handle exception into alert box

How can I coerce an exception message that has bubbled from my business layer into an alert 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 ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM