最佳答案
我有一些基本的代码,以确定我的 MVC 应用程序中的错误。目前在我的项目中,我有一个控制器称为 Error
与行动方法 HTTPError404()
,HTTPError500()
,和 General()
。它们都接受字符串参数 error
。使用或修改下面的代码。
将数据传递给 Error 控制器进行处理的最佳/正确方法是什么?我希望有一个尽可能稳健的解决方案。
protected void Application_Error(object sender, EventArgs e)
{
Exception exception = Server.GetLastError();
Response.Clear();
HttpException httpException = exception as HttpException;
if (httpException != null)
{
RouteData routeData = new RouteData();
routeData.Values.Add("controller", "Error");
switch (httpException.GetHttpCode())
{
case 404:
// page not found
routeData.Values.Add("action", "HttpError404");
break;
case 500:
// server error
routeData.Values.Add("action", "HttpError500");
break;
default:
routeData.Values.Add("action", "General");
break;
}
routeData.Values.Add("error", exception);
// clear error on server
Server.ClearError();
// at this point how to properly pass route data to error controller?
}
}