我的生产服务器不允许显示错误详细信息(因为我猜因为设置自定义错误没有帮助)。
所以我尝试使用应用程序的内置错误处理功能。
我有以下代码:
这是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>
:
proxyquire
这是我的web.config的一部分: ....
当我在http://exmaple.com呼叫我的网站时,它会重定向到地址http://exmaple.com/Error.cshtml?aspxerrorpath=/并自动刷新。 在我的调试环境中,发生错误时错误页面正常呈现并显示异常跟踪。
我想知道如何找到此错误的根本原因或如何查看错误跟踪?