【发布时间】: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