我使用下面的代码实现了异常处理.[编辑开始]我不知道它为什么两次调用视图.[已编辑完成]
Basecontroller
public class BaseController : Controller
{
protected override void OnException(ExceptionContext filterContext)
{
if (filterContext.HttpContext.IsCustomErrorEnabled)
{
filterContext.ExceptionHandled = true;
if (filterContext.Exception.GetType() == typeof(ArgumentOutOfRangeException))
{
this.View("OutOfRange").ExecuteResult(this.ControllerContext);
}
else
{
this.View("Error").ExecuteResult(this.ControllerContext);
}
}
base.OnException(filterContext);
}
}
Run Code Online (Sandbox Code Playgroud)
HomeController的
public class HomeController : BaseController
{
public ActionResult Exception2()
{
throw (new ArgumentOutOfRangeException());
}
public ActionResult Exception3()
{
throw (new Exception());
}
}
Run Code Online (Sandbox Code Playgroud)
错误视图(仅限共享文件夹)
@model System.Web.Mvc.HandleErrorInfo
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<title>Error</title>
</head>
<body>
<h2>
Sorry, an error occurred while …Run Code Online (Sandbox Code Playgroud)