我已经进行了设置,因此如果Exception
抛出 an ,我可以使用我的自定义错误页面显示它。但在某些情况下,我不想被导航到错误页面,而是希望它显示一个简单的对话窗口。
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 示例工作得非常好,我只是想让代码更干净,因为在每个操作(我想显示一个对话框)中都有尝试捕获并不是很漂亮。
我正在使用 .NET MVC 4。