本文介绍了等效于.net Core Webapi 2的HttpResponseException/IHttpActionResponse(不是mvc)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我阅读有关用于响应请求和处理错误的webapi时,一切都基于:

When I am reading about webapi for responding to requests and handling errors everything is based around:

IHttpActionResult
HttpResponseException

但是,当您创建.net核心webapi项目时,这些将不可用.我可以找到IActionResult,这似乎是等效的吗?

But when you create a .net core webapi project these are not available. I can find IActionResult, which seems to be the equivalent?

但是,我正四处转转,​​试图找出一件非常简单的事情,即如何处理.net核心webapi中的错误,因为HttpResponseException不可用.我得到的印象是,带有"http"的所有内容仅适用于完整的MVC应用程序.

But I'm going round in circles trying to find out a really very simple thing, which is how to handle errors in a .net core webapi because HttpResponseException is not available. I get the impression everything with 'http' in it, is just for a full MVC application.

我要做的就是返回一个错误,当然这必须很简单...

All I want to do is return an error, surely it must be simple...

推荐答案

IActionResult等效于IHttpActionResult.这是合并ASP.NET Core MVC中的MVC和Web API的一部分.

IActionResult is the equivalent of IHttpActionResult, as you've suggested. This is part of the consolidation of what was known as MVC and Web API in ASP.NET Core MVC.

对于HttpResponseException,此问题已在ASP.NET Core中完全删除.在GitHub上有一个有趣的 issue ,David Fowler解释了为什么这样做是这样的:

As for HttpResponseException, this has been completely removed in ASP.NET Core. There's an interesting issue around this on GitHub, where David Fowler gives an explanation as to why this is the case:

建议的替代方法是使用各种IActionResult实现来创建JSON响应,返回错误等.再次,从问题出发:

The suggested alternative to this is to use the various IActionResult implementations for creating JSON responses, returning errors, etc. Again, from the issue:

同时支持IActionResult和object的原因是它们各自适合不同的目的和风格.在某些最简单的情况下,使用对象很不错,但它一点也不强大.为了完全控制,有一个IActionResult,它遵循众所周知的命令模式".

The reason that both IActionResult and object are supported is that they each suit different purposes and different styles. For some of the simplest cases, it's nice to use object, but it isn't powerful at all. For full control, there's IActionResult, which follows the well-known "command pattern."

IActionResult模式允许控制器明确声明由于操作而应发生的情况:某种错误代码,重定向,序列化的数据对象,呈现的视图等.

The IActionResult pattern allows the controller to explicitly state what should happen as a result of the action: some kind of error code, a redirect, a serialized data object, a rendered view, etc.

如果您要处理控制器外部的错误,则可能会受益于阅读文档,其中详细介绍了使用中间件或筛选器进行错误处理的细节.在评论中,有一个指向教程的链接详细解释了错误处理的某些方面.

If you're looking to handle errors outside of controllers, you might benefit from reading the docs on this topic, which goes into the details of using either middleware or filters for error-handling. In the comments, there's a link to a tutorial that explains some aspects of error-handling in more detail.

为完整起见,以下是中间件方法教程中的代码:

For completeness, here's the code from the tutorial for the middleware approach:

app.UseExceptionHandler(
 options => {
    options.Run(
    async context =>
    {
      context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
      context.Response.ContentType = "text/html";
      var ex = context.Features.Get<IExceptionHandlerFeature>();
      if (ex != null)
      {
        var err = $"<h1>Error: {ex.Error.Message}</h1>{ex.Error.StackTrace }";
        await context.Response.WriteAsync(err).ConfigureAwait(false);
      }
    });
 }
);

关于IExceptionFilter方法的更多细节,但在此不再赘述.

There are also more details on the IExceptionFilter approach, but I won't repeat all of that here.

这篇关于等效于.net Core Webapi 2的HttpResponseException/IHttpActionResponse(不是mvc)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 20:20