MVC6一般异常处理显示自定义500错误页面

时间:2016-02-16 01:17:30

标签: c# asp.net-core-mvc

我有一个示例代码,

// EmpInfo.cs
try{
    EmpData empData = readFromDB(empID);
}catch (Exception){
    Logger.logError(“Fails to get data for empID: {0}”, empID);
    throw;
}

如何编写以下函数来处理EmpInfo.cs中的异常并显示自定义500错误页面。 由于我使用的是MVC6,它有公告OnActionExecuted函数来处理一般错误。 请提供具体代码。谢谢!

//ExceptionHandlerMiddleware.cs
public class ExceptionHandlerMiddleWare
{
    private readonly RequestDelegate next;
    private readonly ILogger logger;

    public ExceptionHandlerMiddleWare(RequestDelegate next, ILoggerFactory loggerFactory)
    {
        this.next = next;
        this.logger = loggerFactory.CreateLogger<ExceptionHandlerMiddleWare>();
    }

    public async Task Invoke(HttpContext context)
    {

        try {
            await this.next.Invoke(context);
        }
        catch(Exception)
        {
            // How to use OnActionExecuted here????????
         }
    }

}

0 个答案:

没有答案