在 MVC ASP.NET 显示异常消息



我创建了一个自定义异常处理程序...但它只显示修复状态文本..我想在我的类中发生的错误视图上显示真正的异常消息..

public class CustomExceptionHandlerFilter : FilterAttribute, IExceptionFilter
{
public void OnException(ExceptionContext filterContext)
{
filterContext.ExceptionHandled = true;
var msg = filterContext.Exception.Message;
filterContext.Result = new ViewResult()
{
ViewName = "Exception"
};
}
}

你试过filterContext.Exception.StackTrace;吗?

public class CustomExceptionHandlerFilter : FilterAttribute, IExceptionFilter
{
public void OnException(ExceptionContext filterContext)
{
filterContext.ExceptionHandled = true;
//Your Code
//var msg = filterContext.Exception.Message;
//filterContext.Result = new ViewResult()
//{
//    ViewName = "Exception"
//};
//My example
var msg = filterContext.Exception.StackTrace;
ViewResult viewResult = new ViewResult();
viewResult.ViewName = "Exception";
viewResult.ViewData.Add("CatchErrorStackTrace", msg);
//create new controller and view for showing error message.
filterContext.RouteData.Values["controller"] = "Error";
filterContext.RouteData.Values["action"] = "Exception";
filterContext.Result = viewResult;
}
}

~/Error/Exception.cshtml

<div>@(ViewData["CatchErrorStackTrace"] == null ? string.Empty : ViewData["CatchErrorStackTrace"])</div>

最新更新