【发布时间】:2015-03-28 12:31:08
【问题描述】:
我是 MVC 的新手。我正在阅读有关 MVC 中的错误日志记录。
我开始了解HandleErrorAttribute。我对此有疑问。
假设我们将HandleErrorAttribute 放在Action 中。
<!-- language: c# -->
[HandleError(View = "Error")]
public ActionResult Index6()
{
throw new Exception("Something terrible happened.");
return View();
}
并在 web.config 中打开自定义错误
<customErrors mode="On">
</customErrors>
现在,如果该“索引操作”中发生任何未处理的异常,它将显示“视图/共享”文件夹中的 Error.cshtml。
但是我们可以在 web.config 的 customErrors 部分下设置一些配置来实现相同的行为。喜欢
<customErrors mode="On" defaultRedirect="~/Error">
<error statusCode="404" redirect="~/Error/NotFound" />
</customErrors>
顺便提一下,我有一个带有“Index”操作的“ErrorController”,它返回“Views/Shared/Error.cshtml”视图。
那么,我的问题是我们为什么要使用HandleErrorAttribute?使用它有什么好处吗?
谢谢
【问题讨论】:
标签: asp.net-mvc exception-handling custom-error-pages