我已经按照以下教程使用剃刀语法对我的 MVC4 项目进行异常处理:http: //www.devcurry.com/2012/06/aspnet-mvc-handling-exceptions-and-404.html
在遵循它之后,我仍然会遇到运行时错误,因此当我输入错误的 url 时,不会重定向到我的错误页面。当我这样做时,异常被记录下来,并且我通过了我在教程中添加的方法,但最后它转到了以下页面:
http://[ipadress]:9880/Error?aspxerrorpath=/test
并向我展示:
Runtime Error
Description: An exception occurred while processing your request. Additionally, another exception occurred while executing the custom error page for the first exception. The request has been terminated.
本教程和我的项目之间唯一的主要区别是我使用了额外的段语言。
我为错误处理实现了以下代码。
在我的 routeConfig 文件中:
routes.MapRoute(
name: "FailWhale",
url: "{language}/FailWhale/{action}/{id}",
defaults: new { language = "en-US", controller="Error" ,action = "FailWhale", id = UrlParameter.Optional }
);
在我的过滤器配置中:
public class HandleAndLogErrorAttribute : HandleErrorAttribute
{
public override void OnException(ExceptionContext filterContext)
{
var exception = filterContext.Exception;
var httpException = exception as HttpException;
if (httpException != null) LogGateway.For(this).Fatal("Fatal Http exception occured --- " + httpException.Message);
else LogGateway.For(this).Fatal("Fatal exception occured --- " + exception.Message);
base.OnException(filterContext);
}
}
我的 web.config:
<customErrors mode="On" defaultRedirect="Error">
<error statusCode="404" redirect="en-US/FailWhale" />
</customErrors>
我的错误控制器:
public ActionResult FailWhale()
{
Response.StatusCode = 404;
Response.TrySkipIisCustomErrors = true;
LogGateway.For(this).Error("Fatal Http exception occured --- " + Response.StatusCode + ": " + Response.StatusDescription);
return View();
}
我可能在这里监督一些事情?