简体   繁体   English

抛出异常的 IEnumerable 在 ASP.NET 核心 Web ZDB93474238D1043CACE1 中的 OkResult 传递时返回无效的 Json

[英]IEnumerable that throws exception returns invalid Json when passed into OkResult in ASP.NET Core Web API

Start up a new ASP.NET Core Web API project in Visual Studio.在 Visual Studio 中启动一个新的 ASP.NET Core Web API 项目。

Now change the ValuesController.Get() action to be this:现在将 ValuesController.Get() 操作更改为:

// GET api/values
[HttpGet]
public ActionResult<IEnumerable<string>> Get()
{
    var results = new[] {"hi"}.Select<string, string>(x => throw new ArgumentException());
    return Ok(results);
}

When you hit this you get a 200 response with a response body of just [当你点击这个时,你会得到一个 200 响应,响应正文为[

Now if you now change it to enumerate the erroring Select before passing to Ok() it will raise an error correctly and return 500 with details of the error as expected in the default ASP.NET API template:现在,如果您现在将其更改为在传递给 Ok() 之前枚举出错的 Select,它将正确引发错误并返回 500,其中包含默认 ASP.NET ZDB974238714CA8DE634A7CE1D08 模板中预期的错误详细信息

var results = new[] {"hi"}.Select<string, string>(x => throw new ArgumentException());
results.ToList();
return Ok(results);

What is happening here?这里发生了什么?

In the first example, the status code and response headers are sent to the client before the JSON serialisation starts.在第一个示例中,状态代码和响应头在 JSON 序列化开始之前发送到客户端。 Giving the JSON serialiser an IEnumerable<string> results in deferred creation of the collection, which ends up being performed during the serialisation process.给 JSON 序列化器一个IEnumerable<string>会导致集合的延迟创建,最终在序列化过程中执行。 This means that the 200 status code and response headers are sent before the exception is thrown.这意味着 200 状态代码和响应标头在抛出异常之前发送。 Because the status code has already been sent, it cannot be changed from 200. The response cannot be "undone" either - it's a forward-only stream of data.因为状态码已经发送,所以不能从 200 更改。响应也不能“撤消”——它是只转发的 stream 数据。

In the second example, the collection is created before the JSON serialisation process runs.在第二个示例中,集合是在 JSON 序列化过程运行之前创建的。 In this case, the exception is thrown before the call to return Ok(results) can be reached.在这种情况下,在调用return Ok(results)之前抛出异常。 Whatever you're using for global exception handling catches this exception and writes out a response with a status code of 500.无论您使用什么进行全局异常处理,都会捕获此异常并写出状态码为 500 的响应。

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

相关问题 当我在查询中使用“包含”时,ASP.NET Core Web API 返回不完整的 JSON 数据 - ASP.NET Core Web API returns incomplete JSON data when I use 'include' in my query 在ASP.Net Core 2 Web API发生异常时捕获JSON请求 - Capture JSON request when exception on ASP.Net Core 2 Web API ASP.NET Core Web API引发HTTP 500 - ASP.NET Core Web API Throws HTTP 500 从AngularJS调用ASP.NET Web API会引发异常 - Calling ASP.NET Web API from AngularJS throws exception 从网页传递到ASP.NET Web API服务时JSON被截断 - JSON truncated when passed from web page to ASP.NET Web API service ASP.NET Core 5.0 Web API 在尝试使用 HttpDelete 删除记录时抛出错误 405 - ASP.NET Core 5.0 Web API throws error 405 when trying to delete record using HttpDelete 当asp.net核心api方法类型为IEnumerable时如何返回dapper动态查询<dynamic>并且没有 newtonsoft.json 参考</dynamic> - How to return dapper dynamic query when asp.net core api method type is IEnumerable<dynamic> and without newtonsoft.json reference Linux 上 ASP.Net Core Web API 中未处理的异常 - Unhandled Exception in ASP.Net Core Web API on Linux ASP.NET 内核中的全局异常处理程序 Web API 3.1 - Global Exception Handler in ASP.NET Core Web API 3.1 ASP.NET 核心 Web API 必需的属性异常处理 - ASP.NET Core Web API Required Property Exception Handling
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM