2016-04-03 12 views
1

这听起来很奇怪,但我想弄清楚WHERE有关异常的调试信息是从MVC Web API中传输的。每当发生异常时,它返回一个对象(JSON格式因为我除去XML格式化器)具有以下属性:MVC Web API异常处理程序JSON调试信息与OWIN/Kotana

  • 消息

  • ExceptionMessage

  • ExceptionType

  • 堆栈跟踪

WebApiConfig.Register()

config.Services.Replace(typeof(IExceptionHandler), new CustomExceptionHandler()); 

取代现有实现IExceptionHandler与下面的行然而,即使我可以证实,在发生异常时(通过调试断点)新的处理程序被调用时,具有关于错误的调试信息的相同JSON对象被返回。

我的问题是:什么股票系统或机制负责生成和发送此信息?

回答

0

采取的的ExceptionHandler

Handle方法看看Global Error Handling in ASP.NET Web API 2

ExceptionHandlerContext类表示在其中未处理的例外处理发生时

ExceptionHandlerContext.Result

上下文

获取或设置处理异常时提供响应消息的结果。

自定义错误消息异常处理程序

的下方以下产生定制的错误响应到客户端,包括电子邮件地址用于接触支撑。

class OopsExceptionHandler : ExceptionHandler 
{ 
    public override void HandleCore(ExceptionHandlerContext context) 
    { 
     context.Result = new TextPlainErrorResult 
     { 
      Request = context.ExceptionContext.Request, 
      Content = "Oops! Sorry! Something went wrong." + 
         "Please contact [email protected] so we can try to fix it." 
     }; 
    } 

    private class TextPlainErrorResult : IHttpActionResult 
    { 
     public HttpRequestMessage Request { get; set; } 

     public string Content { get; set; } 

     public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken) 
     { 
      HttpResponseMessage response = 
          new HttpResponseMessage(HttpStatusCode.InternalServerError); 
      response.Content = new StringContent(Content); 
      response.RequestMessage = Request; 
      return Task.FromResult(response); 
     } 
    } 
} 

可以使用IHttpActionResult扩展的Web API来创建你想要的任何响应体。