简体   繁体   English

ASP.NET Web API 2 中的全局错误处理

[英]Global Error Handling in ASP.NET Web API 2

Preamble: This question is different from "Exception Handling in ASP.net Web API" as the OP was looking for custom Error Handling, not global handling.序言:这个问题与“ASP.net Web API 中的异常处理”不同,因为 OP 正在寻找自定义错误处理,而不是全局处理。 It also differs from other earlier questions that were answered, as those were for earlier versions of Web API, not version 2. Also note I am going to self answer this question.它也不同于之前回答的其他问题,因为这些问题是针对早期版本的 Web API,而不是版本 2。另外请注意,我将自己回答这个问题。 It took quite some searching to find the correct answer.找了好久才找到正确的答案。

The question is: How do I globally handle errors in Web API 2.0?问题是:如何全局处理 Web API 2.0 中的错误? The error handling I have set up for MVC does not get activated for web api calls and I need to generically handle any error that is thrown so that relevant error information is returned to the client.我为 MVC 设置的错误处理不会为 web api 调用激活,我需要一般性地处理抛出的任何错误,以便将相关错误信息返回给客户端。

Global Error handling is correctly answered in this asp.net article .全局错误处理在这篇 asp.net 文章中得到了正确回答。 However the articles is missing a few important points to make the code actually work.然而,这些文章缺少使代码实际工作的几个要点。

The article covers the details, but here it is in a nutshell:这篇文章涵盖了细节,但这里是简而言之:

  1. Global Error Handling is already included in System.Web.Http.ExceptionHandling The classes in the article are already in this library, so there is no need to rewrite them.全局错误处理已经包含在System.Web.Http.ExceptionHandling文章中的类已经在这个库中,所以不需要重写它们。

  2. The only class you need to write is the one which is customized for your app.您需要编写的唯一类是为您的应用程序定制的类。 In the article, they call it the "OopsExceptionHandler" However, the one written in the article does not compile.在文章中,他们称其为“OopsExceptionHandler”,但是,文章中所写的没有编译。 This is the updated code that does work:这是有效的更新代码:

     public class OopsExceptionHandler : ExceptionHandler { public override void Handle(ExceptionHandlerContext context) { context.Result = new TextPlainErrorResult { //If you want to return the actual error message //Content = context.Exception.Message Request = context.ExceptionContext.Request, Content = "Oops! Sorry! Something went wrong." + "Please contact support@contoso.com 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); } } }
  3. You then need to register the ExceptionHandler.然后,您需要注册 ExceptionHandler。 An example of this is not given in the article, so here it is:文章中没有给出这样的例子,所以这里是:

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

This line goes in the WebApiConfig file, in the register method.此行位于 WebApiConfig 文件中的 register 方法中。 Note that it is using 'Replace' not 'Add' as it will error out on Add.请注意,它使用的是“替换”而不是“添加”,因为它会在添加时出错。 Only one is allowed by the framework.框架只允许一个。

That is all that is required.这就是所需要的全部内容。 To test, throw an error from your web API and you will see the error message returned as the content of the webAPI call.要进行测试,请从您的 Web API 抛出错误,您将看到作为 webAPI 调用内容返回的错误消息。 Note that there are security implications to returning error messages to the client, so make sure this is what you really want.请注意,将错误消息返回给客户端存在安全隐患,因此请确保这是您真正想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM