asp.net-mvc - 显示自定义错误页面并显示异常详细信息

标签 asp.net-mvc asp.net-mvc-4 exception error-handling

我有一个自定义的 Errors Controller ,如下所示:

public class ErrorsController : BaseController
{
    public ActionResult RaiseError(string error = null)
    {
        string msg = error ?? "An error has been thrown (intentionally).";
        throw new Exception(msg);
    }

    public ActionResult Error404()
    {
        Response.TrySkipIisCustomErrors = true;
        Response.StatusCode = (int)HttpStatusCode.NotFound;
        return View();
    }

    public ActionResult Error500()
    {
        Response.TrySkipIisCustomErrors = true;

        var model = new Models.Errors.Error500()
        {
            ServerException = Server.GetLastError(),
            HTTPStatusCode = Response.StatusCode
        };

        return View(model);
    }
}

我的 Errors500.cshtml 看起来像这样:
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Error500</title>
</head>
<body>
    <div>
        An internal error has occurred.
            @if (Model != null && Model.ServerException != null &&                 HttpContext.Current.IsDebuggingEnabled)
    {
        <div>
            <p>
                <b>Exception:</b> @Model.ServerException.Message<br />
            </p>
            <div style="overflow:scroll">
                <pre>
                    @Model.ServerException.StackTrace
                </pre>
            </div>
        </div>
    }
    </div>

</body>
</html>

并且我的 web.config 指定了我的错误处理程序:
<system.webServer>
<httpErrors errorMode="Custom" existingResponse="Replace" >
  <remove statusCode="404" subStatusCode="-1" />
  <error statusCode="404" subStatusCode="-1" responseMode="ExecuteURL" path="/Errors/Error404" />
  <remove statusCode="500" subStatusCode="-1" />
  <error statusCode="500" subStatusCode="-1" responseMode="ExecuteURL" path="/Errors/Error500" />
</httpErrors>

问题是:每次我调用/errors/raiseerror 来测试我的 500 处理时;我被重定向到错误/错误 500(很好)。但是,异常数据不会呈现在页面上,因为 Server.GetLastError() 调用返回 null 而不是 RaiseError() 抛出的异常。

处理自定义 500 错误页面的最佳方法是什么,该自定义页面也可以呈现异常详细信息?

最佳答案

最简单的方法是:

使用 MVC 的内置支持来处理异常。默认情况下,MVC 使用 HandleErrorAttribute 注册于 App_Start\FilterConfig.cs :

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
    filters.Add(new HandleErrorAttribute());
}

现在确保您有一个名为 Error 的 View 。在 Views\Shared文件夹。默认情况下, View 的模型类型为 HandleErrorInfo具有名为 Exception 的属性.如果需要,您可以显示异常消息和其他详细信息:

Error.cshtml
@model HandleErrorInfo

@if(Model != null)
{       
    @Model.Exception.Message
}

您可以自定义Error.cshtml按您想要的方式翻页...

关于asp.net-mvc - 显示自定义错误页面并显示异常详细信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19735740/

相关文章:

c# - ASP.Net Mvc - View 调用可能导致数据检索的函数是否可以接受?

c# - ASP.NET MVC - 查看位置问题 : The view 'Index' or its master was not found

c# - 如何使用 Jquery Mobile 在 MVC 中动态创建带有 Count Bubbles 的链接

具有不同参数的java测试构造函数

java - 当我尝试在finally block 中关闭BufferedReader时,为什么eclipse会提示?

asp.net-mvc - ValidationSummary 显示重复的消息

c# - 无法在非静态上下文中访问静态方法

c# - 自动刷新缓存 ASP.NET

html - 使用 @html.ActionLink 的子菜单

ios - 容器 View Controller 应用程序在 Swift 中崩溃 NSInternalInconsistencyException?