MVC4 jQueryMobile将不显示自定义错误页onException的 [英] MVC4 jQueryMobile won't show custom error page OnException

查看:135
本文介绍了MVC4 jQueryMobile将不显示自定义错误页onException的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得一个自定义错误页从MVC4移动应用程序显示,但保持刚开错误加载页面显示,而不是我的自定义页面的黄色信息。

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.

我已经尝试使用在 HandleErrorAttribute 下面上的操作,控制器,但没有成功。

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

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

我也试图重写我的基本控制器的onException的方法,但这也似乎没有任何效果。

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

如果我试图在他们预期,只是没有在我的移动应用程序!工作非jQueryMobile MVC4应用这些方法

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??

推荐答案

您可能需要在你的过滤器,以检查是否请求是通过AJAX和返回 JsonResult 而不是一个的ViewResult ,是这样的:

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

然后你不得不间preT适当的方式在客户端的Ajax响应。您还可以发送不同的内容,如果它是一个Ajax请求,像一个标准的 {成功:T / F,消息:Exception.Message} 对象,并设置响应状态codeS适当为好。

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.

这篇关于MVC4 jQueryMobile将不显示自定义错误页onException的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆