In my MVC app I have the following Filter registered to handle unhandled errors. The server side returns a 500 error which is caught by the jquery error handler.

在我的MVC应用程序中,我注册了以下过滤器来处理未处理的错误。服务器端返回500错误,该错误由jquery错误处理程序捕获。

When I hit my page from the same machine as the host IIS I get the expected behavior where I get the error message in my json which I can display to the user. But when I try to hit my app from a different box I get a generic server error page. I have tried turning on/off the custom errors with no effects. The controller action is decorated with the error handler attribute.

当我从与主机IIS相同的机器上打我的页面时,我得到了预期的行为,我在json中收到错误消息,我可以向用户显示。但是当我尝试从另一个盒子中点击我的应用程序时,我得到一个通用服务器错误页面。我试过打开/关闭自定义错误没有任何影响。控制器操作使用错误处理程序属性进行修饰。

public class ErrorHandlerAttribute :  FilterAttribute, IExceptionFilter
    {
        public void OnException(ExceptionContext filterContext)
        {
            filterContext.ExceptionHandled = true;
            filterContext.HttpContext.Response.Clear();
            filterContext.HttpContext.Response.StatusCode = 500;

            filterContext.Result = new JsonResult
            {
                Data = new { message = filterContext.Exception.Message },
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            };
        }
    }

1 个解决方案

#1


0

Need the following line added to the Exception handling filter for the default IIS 500 error handling to not kick in.

需要将以下行添加到异常处理筛选器中,以使默认IIS 500错误处理无法启动。

filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;

filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;

The Filter now looks as follows

过滤器现在看起来如下

public class ErrorHandlerAttribute : FilterAttribute, IExceptionFilter
{
    public void OnException(ExceptionContext filterContext)
    {
        filterContext.ExceptionHandled = true;
        filterContext.HttpContext.Response.Clear();
        filterContext.HttpContext.Response.StatusCode = 500;
        filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;
        filterContext.Result = new JsonResult
        {
            Data = new { error = true,message = filterContext.Exception.Message },
            JsonRequestBehavior = JsonRequestBehavior.AllowGet
        };
    }
}

更多相关文章

  1. 点击JSON数据加载Galleria画廊。我需要新鲜的眼睛来看我的错误
  2. jQuery:离线后发布错误(iOS和Chrome)
  3. 在线请教调用Jquery错误:TypeError: a is undefined 的错误原因
  4. 在发出xml Ajax请求时获取错误412
  5. jquery为属性过滤器动态添加值?
  6. jQuery学习笔记--选择器、过滤器片
  7. Internet Explorer导致无效的真实性令牌错误
  8. DataTables警告:table id = DataTables_Table_0 - Ajax错误。有
  9. 如何在使用jquery验证和自定义错误放置时清除错误

随机推荐

  1. 样式和主题-style&them
  2. Android7.0中文文档(API)-- Toast
  3. Android下修改ImageButton样式
  4. 解决Could not find manifest-merger.jar
  5. 如何进行Android单元测试
  6. Android SDK 更新失败解决 Failed to fet
  7. 2013.09.02(2)——— android 耳机意外拔
  8. Android 单元测试链接整理
  9. android分割线
  10. ch023 Android ContentProvider(第二部分