简体   繁体   English

将异常处理到警报框中

[英]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? 如何强制从业务层冒入的异常消息进入JavaScript的警报框?

BTW, I'm using ASP.NET MVC 1.0. 顺便说一句,我正在使用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. 在OnActionExecuted()的替代版本中,必须将filterContext.Result设置为某些RedirectResult / RedirectRouteResult,以便将任何内容返回到浏览器,因为异常已将其设置为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: 因此,重定向到一些错误页面或控制器操作,该操作将使用警告的javascript呈现html :)这是您可以从以下内容开始的自定义ActionFilter属性:

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 ? 有点混乱,但我希望能回答您的问题?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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