【问题标题】:MVC4 jQueryMobile won't show custom error page OnExceptionMVC4 jQueryMobile 不会显示自定义错误页面 OnException
【发布时间】:2012-11-06 21:26:27
【问题描述】:

我正在尝试从 MVC4 移动应用程序显示自定义错误页面,但一直显示“错误加载页面”黄色消息而不是我的自定义页面。

我已尝试在动作和控制器上使用如下 HandleErrorAttribute,但没有成功

[HandleError(ExceptionType = typeof(SqlException), View = "DatabaseError")]

我也尝试过覆盖基本控制器的 OnException 方法,但这似乎也没有任何效果。

 protected override void OnException(ExceptionContext filterContext)
        {
            if (filterContext == null)
                base.OnException(filterContext);

            Logger.LogException(filterContext.Exception);

            if (filterContext.Exception is SqlException)
                {
                filterContext.Result = new ViewResult { ViewName = "DatabaseError" };
                }

            if (filterContext.Exception is SomeOtherException)
                {
                filterContext.Result = new ViewResult { ViewName = "Error" };
                }

            if (filterContext.HttpContext.IsCustomErrorEnabled)
                {
                filterContext.ExceptionHandled = true;
                filterContext.Result.ExecuteResult(this.ControllerContext);
                }
        }

如果我在非 jQueryMobile MVC4 应用程序上尝试这些方法,它们会按预期工作,只是在我的移动应用程序中不行!

任何人对为什么以及如何使这项工作有任何见解?

【问题讨论】:

    标签: jquery-mobile asp.net-mvc-4


    【解决方案1】:

    如果请求是通过 AJAX 进行的,您可能需要检查您的过滤器并返回 JsonResult 而不是 ViewResult,类似于:

    public class TypeSwitchingHandleErrorAttribute : HandleErrorAttribute
    {
        private static readonly string[] AJAX_ACCEPT_TYPES = new[] { "application/json", "application/javascript", "application/xml" };
    
        private bool IsAjax(ExceptionContext filterContext)
        {
            return filterContext.HttpContext.Request.Headers["X-Requested-With"] == "XMLHttpRequest"
                ||
                filterContext.HttpContext.Request.AcceptTypes.ContainsAny(AJAX_ACCEPT_TYPES);
        }
    
        private void setResult(ExceptionContext filterContext, object content)
        {
            if( IsAjax(filterContext) )
            {
                filterContext.Result = new JsonResult { Data = content, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
            } else
            {
                filterContext.Result = new ViewResult { ViewName = (string)content };
            }
        }
    
        public override void OnException(ExceptionContext filterContext)
        {
            // your code...then where you set the result...
            setResult(filterContext, "DatabaseError etc");
        }
    }
    

    然后您必须在客户端适当地解释 ajax 响应。如果是 ajax 请求,您还可以发送不同的内容,例如标准的 {success: t/f, message: Exception.Message } 对象,并适当地设置响应状态代码。

    【讨论】:

      【解决方案2】:

      好的,现在可以通过禁用 Ajax 来显示相应的错误页面!

      在我的 _layout.cshtml 页面中,我添加了以下 javascript:

       $.mobile.ajaxEnabled = false;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-07-13
        • 1970-01-01
        • 2013-09-12
        • 1970-01-01
        • 2020-10-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多