方案1
- 在
web.config
中添加customErrors
节点 - 在操作方法上添加
HandleError
特性
<system.web>
<customErrors mode="On" />
</system.web>
[HandleError]
[HandleError(ExceptionType = typeof(Exception), View = "~/Views/Home/Index.cshtml")]
public ActionResult Do()
{
throw new NotImplementedException();
}
方案2
- 在
web.config
中添加customErrors
节点,同上 - 重写
Controller
的OnException
方法
protected override void OnException(ExceptionContext filterContext)
{
//标识异常已经处理
filterContext.ExceptionHandled = true;
filterContext.Result = RedirectToAction("Error", "InternalError");
// OR return specific view
/*
filterContext.Result = new ViewResult
{
ViewName = "~/Views/Error/InternalError.cshtml"
};
*/
}
方案3
- 在
web.config
中添加customErrors
节点,同上 自定义特性
类继承自HandleErrorAttribute
,IExceptionFilter
并实现OnException
方法- 在操作方法上添加
自定义特性
方案4
在Global.asax.cs
中进行处理
protected void Application_Error(object sender, EventArgs e)
{
//处理异常
}
ASP.NET MVC异常处理方案 https://www.cnblogs.com/yanglang/p/6961417.html
https://www.tutorialsteacher.com/mvc/exception-handling-in-mvc