简体   繁体   中英

Asp.Net Mvc Display exception in View from tempdata

I am handling error in Base controller. I need to display the error stored in tempdata, Exception type in a razor view. How can I do that?

Base Controller code

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();
}

Razor View Code

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

Try to make common exception attribute handling and register it as global filters. Like,

Common Exception Handling attribute :

    /// <summary>
    /// This action filter will handle the errors which has http response code 500. 
    /// As Ajax is not handling this error.
    /// </summary>
    [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<HandleErrorInfo>(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<HandleErrorInfo>(model),
                TempData = filterContext.Controller.TempData,
            };

            result.TempData["Exception"] = filterContext.Exception;

            return result;
        }
    }

And Error/_Error view

@model HandleErrorInfo
<div>
    This is the error Description
    @TempData["Exception"]
 </div>

I agree that you should never expose an exception to your view but if you really need to, try using a custom attribute.

 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();
        }
    }

If you override the OnException method in the base controller, then every action will get an Exception object placed in temp data. This maybe the desired behavior but with an attribute you can selectively enable this feature.

I would strongly suggest not to show any detailed exception information in any public facing application as this could end up as a security issue. However, if this is an intranet application with controlled access or if you REALLY want to show the exception details, create a DisplayTemplate and use it as follows:

<div>
Exception Details
@Html.Display(TempData["Exception"])
</div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM