MVC4 jQueryMobile won't show custom error page OnException

与世无争的帅哥 提交于 2019-12-12 04:35:50

问题


I'm trying to get a custom error page to display from a MVC4 Mobile Application but keep just getting the "Error Loading Page" yellow message being displayed instead of my custom page.

I have tried using the HandleErrorAttribute as below on Actions and Controllers with no success

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

I have also tried overriding the OnException method of my base controller but this also doesn't appear to have any effect.

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

If I try these methods on a non jQueryMobile MVC4 application they work as expected, just not in my mobile application!

Anyone have any insight as to why and how to make this work??


回答1:


Ok so by disabling Ajax the appropriate error pages now get displayed!

In my _layout.cshtml page I added the following javascript:

 $.mobile.ajaxEnabled = false;



回答2:


You probably need to check in your filter if the request is via AJAX and return a JsonResult instead of a ViewResult, something like:

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

Then you'd have to interpret the ajax response appropriately on client-side. You could also send different content if it's an ajax request, like a standard {success: t/f, message: Exception.Message } object, and set the response status codes appropriately as well.



来源:https://stackoverflow.com/questions/13445643/mvc4-jquerymobile-wont-show-custom-error-page-onexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!