本文介绍了MVC 5 - 如何显示共享视图Error.cshtml异常消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个新的MVC 5项目启动,在web.config中的设置的customErrors模式=允许共享视图Error.cshtml显示,当我强迫(加薪)的异常,但它只显示了以下文字...

If I start with a new MVC 5 project, in web.config setting customErrors mode="on" allows the shared view 'Error.cshtml' to show when I force (raise) an exception, but it only shows the following text...

错误。

处理您的请求时出现错误。

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

How do I pass information to this view to display more relevant info, such as what error occurred? Can I use this view if I use the Global.asax method...

protected void Application_Error()

推荐答案

覆盖过滤器:

// 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)
        {
            var exception = filterContext.Exception;
            string controller = "";
            string action = "";


                controller = filterContext.RouteData.Values["controller"].ToString();
                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;
        }
  }

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

Declare the model at the top of the 'error' view:

@model System.Web.Mvc.HandleErrorInfo

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

Then use on the page like so:

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

希望这有助于。

这篇关于MVC 5 - 如何显示共享视图Error.cshtml异常消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 15:24