C# ASP.NET MVC应用程序的错误处理未正确显示错误页
我的生产服务器不允许显示错误详细信息(我猜,因为关闭自定义错误没有帮助)。 所以我尝试使用应用程序的内置错误处理功能。 我有以下代码: 这在C# ASP.NET MVC应用程序的错误处理未正确显示错误页,c#,asp.net-mvc,asp.net-mvc-4,error-handling,stack-trace,C#,Asp.net Mvc,Asp.net Mvc 4,Error Handling,Stack Trace,我的生产服务器不允许显示错误详细信息(我猜,因为关闭自定义错误没有帮助)。 所以我尝试使用应用程序的内置错误处理功能。 我有以下代码: 这在global.asax中: protected void Application_Error(object sender, EventArgs e) { //Exception exception = Server.GetLastError(); //Server.ClearError(); //Response.Redirect("
global.asax中:
protected void Application_Error(object sender, EventArgs e)
{
//Exception exception = Server.GetLastError();
//Server.ClearError();
//Response.Redirect("/Home/Error");
var httpContext = ((MvcApplication)sender).Context;
var currentController = " ";
var currentAction = " ";
var currentRouteData = RouteTable.Routes.GetRouteData(new HttpContextWrapper(httpContext));
if (currentRouteData != null)
{
if (currentRouteData.Values["controller"] != null &&
!String.IsNullOrEmpty(currentRouteData.Values["controller"].ToString()))
{
currentController = currentRouteData.Values["controller"].ToString();
}
if (currentRouteData.Values["action"] != null &&
!String.IsNullOrEmpty(currentRouteData.Values["action"].ToString()))
{
currentAction = currentRouteData.Values["action"].ToString();
}
}
var ex = Server.GetLastError();
var controller = new HomeController();
var routeData = new RouteData();
var action = "Error";
var statusCode = 500;
if (ex is ArgumentException)
{
action = "NotFound";
statusCode = 404;
}
httpContext.ClearError();
httpContext.Response.Clear();
httpContext.Response.StatusCode = statusCode;
httpContext.Response.TrySkipIisCustomErrors = true;
routeData.Values["controller"] = "Error";
routeData.Values["action"] = action;
controller.ViewData.Model = new HandleErrorInfo(ex, currentController, currentAction);
((IController)controller).Execute(new RequestContext(new HttpContextWrapper(httpContext), routeData));
}
这是我的Error.cshtml
,它位于Views\Shared\Error.cshtml
中:
@model System.Web.Mvc.HandleErrorInfo
@{
ViewBag.Title = "Error";
}
<hgroup class="title">
<h1 class="error">Error.</h1>
<h2 class="error">An error occurred while processing your request.</h2>
@if (Model != null)
{
<h4>@Model.Exception.Message</h4>
<h5>@Model.Exception.StackTrace</h5>
}
else
{
<h1> error</h1>
}
</hgroup>
@model System.Web.Mvc.HandleErrorInfo
@{
ViewBag.Title=“错误”;
}
错误。
处理您的请求时出错。
@如果(型号!=null)
{
@Model.Exception.Message
@Model.Exception.StackTrace
}
其他的
{
错误
}
下面是my web.config的部分内容:
当我调用我的站点时,它将重定向到地址,并在内部自动刷新。
在我的调试环境中,当发生错误时,错误页将正常呈现并显示异常跟踪
我想知道如何找到此错误的根本原因,或者如何查看错误跟踪?胡乱猜测-可能您的错误页面本身抛出了一个错误并导致无限循环。尝试在System.Web.Mvc.HandleErrorInfo
模型中放置一些属性,这些属性计算显示的异常信息并将所有内容放入Try。。捕获
块,以便不会生成异常。@Alexei我曾尝试对任何代码进行注释,但以下是服务器支持屏幕截图告诉我的,即使在进行此更改后:“处理请求时发生异常。此外,在执行自定义错误页时发生另一异常…”
能否检查以下代码在global.asax.cs
中是否返回错误?:protectedvoid应用程序_error(){Exception Exception=Server.GetLastError();//将错误记录在某个地方,以便以后检查}
。您还可以检查事件查看器,因为未处理的IIS异常显示为警告。