从tempdata查看Asp.Net Mvcexception

我在Base控制器中处理错误。 我需要在剃刀视图中显示存储在tempdata,Exception类型中的错误。 我怎样才能做到这一点?

基本控制器代码

protected override void OnException(ExceptionContext filterContext) { // if (filterContext.ExceptionHandled) // return; //Let the request know what went wrong filterContext.Controller.TempData["Exception"] = filterContext.Exception.Message; //redirect to error handler filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary( new { controller = "Error", action = "Index" })); // Stop any other exception handlers from running filterContext.ExceptionHandled = true; // CLear out anything already in the response filterContext.HttpContext.Response.Clear(); } 

剃刀查看代码

 
This is the error Description @Html.Raw(Html.Encode(TempData["Exception"]))

尝试进行常见的exception属性处理并将其注册为全局filter。 喜欢,

Common Exception Handling属性:

  ///  /// This action filter will handle the errors which has http response code 500. /// As Ajax is not handling this error. ///  [AttributeUsage(AttributeTargets.Class)] public sealed class HandleErrorAttribute : FilterAttribute, IExceptionFilter { private Type exceptionType = typeof(Exception); private const string DefaultView = "Error"; private const string DefaultAjaxView = "_Error"; public Type ExceptionType { get { return this.exceptionType; } set { if (value == null) { throw new ArgumentNullException("value"); } this.exceptionType = value; } } public string View { get; set; } public string Master { get; set; } public void OnException(ExceptionContext filterContext) { if (filterContext == null) { throw new ArgumentNullException("filterContext"); } if (!filterContext.IsChildAction && (!filterContext.ExceptionHandled && filterContext.HttpContext.IsCustomErrorEnabled)) { Exception innerException = filterContext.Exception; // adding the internal server error (500 status http code) if ((new HttpException(null, innerException).GetHttpCode() == 500) && this.ExceptionType.IsInstanceOfType(innerException)) { var controllerName = (string)filterContext.RouteData.Values["controller"]; var actionName = (string)filterContext.RouteData.Values["action"]; var model = new HandleErrorInfo(filterContext.Exception, controllerName, actionName); // checking for Ajax request if (filterContext.HttpContext.Request.IsAjaxRequest()) { var result = new PartialViewResult { ViewName = string.IsNullOrEmpty(this.View) ? DefaultAjaxView : this.View, ViewData = new ViewDataDictionary(model), TempData = filterContext.Controller.TempData }; filterContext.Result = result; } else { var result = this.CreateActionResult(filterContext, model); filterContext.Result = result; } filterContext.ExceptionHandled = true; } } } private ActionResult CreateActionResult(ExceptionContext filterContext, HandleErrorInfo model) { var result = new ViewResult { ViewName = string.IsNullOrEmpty(this.View) ? DefaultView : this.View, MasterName = this.Master, ViewData = new ViewDataDictionary(model), TempData = filterContext.Controller.TempData, }; result.TempData["Exception"] = filterContext.Exception; return result; } } 

和Error / _Error视图

 @model HandleErrorInfo 
This is the error Description @TempData["Exception"]

我强烈建议不要在任何面向公众的应用程序中显示任何详细的exception信息,因为这可能最终成为一个安全问题。 但是,如果这是具有受控访问权限的Intranet应用程序,或者您真的想要显示exception详细信息,请创建一个DisplayTemplate并按如下方式使用它:

 
Exception Details @Html.Display(TempData["Exception"])

我同意您不应该在视图中公开exception,但如果您确实需要,请尝试使用自定义属性。

  public class CustomExceptionAttribute : System.Web.Mvc.HandleErrorAttribute { public override void OnException(System.Web.Mvc.ExceptionContext filterContext) { if (!filterContext.ExceptionHandled) { filterContext.Controller.TempData.Add("Exception", filterContext.Exception); filterContext.ExceptionHandled = true; } } } public class MyController : System.Web.Mvc.Controller { [CustomException] public ActionResult Test() { throw new InvalidOperationException(); } } 

如果覆盖基本控制器中的OnException方法,则每个操作都将获得放置在临时数据中的Exception对象。 这可能是所需的行为,但通过属性,您可以有选择地启用此function。