代码之家  ›  专栏  ›  技术社区  ›  user137348

如何摆脱中的HTML错误报告ASP.NETMVC?

  •  6
  • user137348  · 技术社区  · 16 年前

    我所需要的只是纯文本的错误信息。但是ASP.NET正在对每个错误执行一些HTML报告输出。

    我有一个jqueryajax调用,当抛出一个错误时,我会把所有的废话都交给客户端。

    我创建了一个过滤器属性,但没有帮助。

    public class ClientErrorHandler : FilterAttribute, IExceptionFilter
    {
        public void OnException(ExceptionContext filterContext)
        {
            var responce = filterContext.RequestContext.HttpContext.Response;
            responce.Write(filterContext.Exception.Message);
            responce.ContentType = MediaTypeNames.Text.Plain;
            filterContext.ExceptionHandled = true;
        }
    }
    

    编辑

    我看到了 this

    我想看看这里有什么 filterContext.Exception.Message

    3 回复  |  直到 16 年前
        1
  •  2
  •   marcind    16 年前

    在我看来,无法正确处理异常的原因是因为它发生在MVC管道之外。如果您查看发布的代码中的堆栈跟踪,则没有引用系统.Web.Mvc代码(当异常发生时触发异常过滤器是从 ControllerActionInvoker.InvokeAction ).

    堆栈跟踪指示异常发生在ASP.NET管道(OnEndRequest)和它通过Autofac组件。

    Error 事件。请参阅以下有关创建全局错误处理程序的文章: http://msdn.microsoft.com/en-us/library/994a1482.aspx . 在这种情况下,您可以处理错误并重定向到自定义错误页。

        2
  •  0
  •   Sruly    16 年前

    ContentResult result = new ContentResult();
    result.Content = filterContext.Exception.Message;
    result.ContentType = MediaTypeNames.Text.Plain; 
    filterContext.Result = result;
    filterContext.ExceptionHandled = true; 
    
        3
  •  0
  •   Ta01    16 年前

    between jQuery and WCF -如果你能做到的话,你可能不得不重新设计你的服务。