C# ASP MVC OneException发送异常数据以查看
我正在重写我的控制器OneException方法C# ASP MVC OneException发送异常数据以查看,c#,asp.net-mvc,C#,Asp.net Mvc,我正在重写我的控制器OneException方法 protected override void OnException(ExceptionContext filterContext) { if (filterContext.ExceptionHandled) { return; } filterContext.Result = new ViewResult {
protected override void OnException(ExceptionContext filterContext)
{
if (filterContext.ExceptionHandled)
{
return;
}
filterContext.Result = new ViewResult
{
ViewName = "ErrorPage",
};
filterContext.ExceptionHandled = true;
}
这让我毫不费力地看到了我的观点。但是,我也希望能够将异常消息发送到视图。大概是这样的:
ViewBag.Message = filterContext.Exception.Message;
在我看来:
@model System.Web.Mvc.HandleErrorInfo
@{
ViewBag.Title = "Error";
}
<h1 class="text-danger">Error.</h1>
<h2 class="text-danger">An error occurred while processing your request.</h2>
<h2 class="text-danger">Error message: @ViewBag.Message</h2>
你把
ViewBag.Message=filterContext.Exception.Message放在哪里了?你到底遇到了什么问题?你没有看到异常信息吗?我已经更新了操作。结果我犯了个错误。
protected override void OnException(ExceptionContext filterContext)
{
if (filterContext.ExceptionHandled)
{
return;
}
var ex = filterContext.Exception ?? new Exception("No further information exists.");
ViewBag.Message = ex.Message;
filterContext.Result = View("ErrorPage");
filterContext.ExceptionHandled = true;
}