【问题标题】:How do I show Exception message in shared view Error.cshtml?如何在共享视图 Error.cshtml 中显示异常消息?
【发布时间】:2015-10-05 03:14:09
【问题描述】:

如果我从一个新的 MVC 5 项目开始,在 web.config 设置 customErrors mode="on" 允许共享视图“Error.cshtml”在我强制(引发)异常时显示,但它只显示以下内容文字...

错误。

处理您的请求时出错。

如何将信息传递到此视图以显示更多相关信息,例如发生了什么错误?如果我使用 Global.asax 方法,我可以使用这个视图吗...

protected void Application_Error()

?

【问题讨论】:

    标签: c# asp.net-mvc razor error-handling asp.net-mvc-5


    【解决方案1】:

    覆盖过滤器:

    // In your App_Start folder
    public class FilterConfig
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new ErrorFilter());
            filters.Add(new HandleErrorAttribute());
            filters.Add(new SessionFilter());
        }
    }
    
    // In your filters folder (create this)
    public class ErrorFilter : System.Web.Mvc.HandleErrorAttribute
    {
        public override void OnException(System.Web.Mvc.ExceptionContext filterContext)
        {
            System.Exception exception = filterContext.Exception;
            string controller = filterContext.RouteData.Values["controller"].ToString();;
            string action = filterContext.RouteData.Values["action"].ToString();
    
            if (filterContext.ExceptionHandled)
            {
                return;
            }
            else
            {
                // Determine the return type of the action
                string actionName = filterContext.RouteData.Values["action"].ToString();
                Type controllerType = filterContext.Controller.GetType();
                var method = controllerType.GetMethod(actionName);
                var returnType = method.ReturnType;
    
                // If the action that generated the exception returns JSON
                if (returnType.Equals(typeof(JsonResult)))
                {
                    filterContext.Result = new JsonResult()
                    {
                        Data = "DATA not returned"
                    };
                }
    
                // If the action that generated the exception returns a view
                if (returnType.Equals(typeof(ActionResult))
                    || (returnType).IsSubclassOf(typeof(ActionResult)))
                {
                    filterContext.Result = new ViewResult
                    {
                        ViewName = "Error"
                    };
                }
            }
    
            // Make sure that we mark the exception as handled
            filterContext.ExceptionHandled = true;
        }
    }
    

    在“错误”视图的顶部声明模型:

    @model System.Web.Mvc.HandleErrorInfo
    

    然后像这样在页面上使用:

    @if (Model != null)
    {
        <div>
            @Model.Exception.Message
            <br />
            @Model.ControllerName
        </div>
    }
    

    希望这会有所帮助。

    【讨论】:

    • 感谢 Denys - 您的解决方案运行良好。为了满足我的特殊需要,我稍微精简了代码,但是您的示例开箱即用...
    • 无法让它工作。它无法解析SessionFilter,即使我删除了该行,Error.cshtml 中的模型也始终为空。我逐字复制了您的代码。知道出了什么问题吗?如果有任何区别,我正在使用 MVC5。
    • Ignore SessionFilter - 这是您可以创建的另一个过滤器。逐步检查过滤器以确保它调用您的“错误”视图。必须创建共享视图,名称应为“错误”(设置新视图结果时与视图名称匹配)。
    • 甚至没有引用变量异常、控制器和操作。他们在这段代码中的目的是什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-01
    • 2020-05-30
    • 2019-09-18
    • 1970-01-01
    相关资源
    最近更新 更多