【问题标题】:Catching exceptions for displaying info捕获显示信息的异常
【发布时间】:2012-10-19 02:57:03
【问题描述】:

我已经进行了设置,因此如果抛出 Exception,我可以在我的自定义错误页面中显示它。但在某些情况下,我不想被导航到错误页面,而是希望它显示一个简单的对话窗口。

public ActionResult Page1()
{
    //The custom error page shows the exception, if one was thrown

    throw new Exception( "An exception was thrown" );

    return View();
}


public ActionResult Page2()
{
    //A dialog should show the exception, if one was thrown

    try
    {
        throw new Exception( "An exception was thrown" );
    }
    catch( Exception ex )
    {
        ViewData["exception"] = ex;
    }
    return View();
}

是否可以使用 CustomAttribute 来处理在 Controller 操作中引发的异常?如果我将CatchException 添加到Page2,我是否可以在每次引发异常时自动将异常存储在ViewData 中的过程。我对 CustomAttributes 没有太多经验,如果您能帮助我,我将不胜感激。

Page2 示例工作得非常好,我只是想让代码更简洁,因为在每个操作(我想显示一个对话框)中都有 try catch 并不是很漂亮。

我正在使用 .NET MVC 4。

【问题讨论】:

    标签: c# asp.net-mvc exception exception-handling


    【解决方案1】:

    您可以创建 Exception 类的子类,并在您的 Page 2 中捕获它

    internal class DialogException : Exception
    {}
    
    public ActionResult Page2()
    {
        //This should a dialog if an exception was thrown
    
        try
        {
            //throw new Exception( "An exception was thrown, redirect" );
            throw new DialogException( "An exception was thrown, show dialog" );
        }
        catch( DialogException ex )
        {
            ViewData["exception"] = ex;
        }
        return View();
    }
    

    【讨论】:

    • 问题是我已经有一些自定义异常类型,它们具有特定的属性。所以我不能真正创建一个新的DialogException 并继承异常。
    【解决方案2】:

    您可以创建一个基本控制器来捕获异常并为您处理它。 此外,看起来控制器已经有一种机制可以为您做到这一点。您必须覆盖控制器内的 OnException 方法。你可以在这里得到一个很好的例子: Handling exception in ASP.NET MVC

    此外,关于如何在此处使用 OnException 的另一个答案: Using the OnException

    通过使用它,你的代码会更干净,因为你不会做很多 try/catch 块。

    您必须过滤要处理的异常。像这样:

    protected override void OnException(ExceptionContext contextFilter)
    {
        // Here you test if the exception is what you are expecting
        if (contextFilter.Exception is YourExpectedException)
        {
            // Switch to an error view
            ...
        }
        //Also, if you want to handle the exception based on the action called, you can do this:
        string actionName = contextFilter.RouteData.Values["action"];
        //If you also want the controller name (not needed in this case, but adding for knowledge)
        string controllerName = contextFilter.RouteData.Values["controller"];
        string[] actionsToHandle = {"ActionA", "ActionB", "ActionC" };
    
        if (actionsTohandle.Contains(actionName)) 
        {
             //Do your handling.
        }
    
        //Otherwise, let the base OnException method handle it.
        base.OnException(contextFilter);
    }
    

    【讨论】:

    • 从被覆盖的 OnException 中我不知道我是想捕捉它并显示一个对话框,还是让它进入错误页面。是否显示错误页面或对话框取决于操作而不是异常类型,因此如果异常是 NullReferenceException 则我无法在 OnException 中进行检查,那么它应该“爆炸”。你能理解我的想法吗?
    • 是的,我明白了。这就是为什么您必须过滤要处理的异常。我将编辑代码以说明我的意思。
    • 哦,当然。我可以比较动作名称。谢谢你:)
    猜你喜欢
    • 2014-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-18
    • 2018-02-12
    • 2012-12-12
    • 1970-01-01
    相关资源
    最近更新 更多