首页 > 解决方案 > 为什么 asp.net core 无法反序列化这个 json?

问题描述

dotnet --version
2.2.300

创建一个空文件夹。在这个文件夹中运行

dotnet new webapi

转到Startup.cs并注释掉app.UseHttpsRedirection();运行:

dotnet run

等待进程启动。假设您使用的是较新的 Windows 10 版本,并且在curl.exe运行 Windows 10 时开箱即用:

 curl.exe http://localhost:5000/api/values -X POST -H "Content-Type: application/json" -d "{}"

观察结果:

{"errors":{"":["Unexpected character encountered while parsing value: {. Path '', line 1, position 1."]},"title":"One or more validation errors occurred.","status":400,"traceId":"0HLN4K718DVA4:00000001"}

在您的情况下,跟踪 ID 会有所不同。

这是来自类似但不同的应用程序的一些调试输出。不同之处在于日志配置,略有不同但相似的POST有效负载,略有不同但相似Content-Type的标头,不同的路由,端口。

[14:02:03 DBG 7] Request matched endpoint 'test.Controllers.ValuesController.Post (test)'
[14:02:03 INF 7] Executing endpoint 'test.Controllers.ValuesController.Post (test)'
[14:02:03 INF 7] Route matched with {action = "Post", controller = "Values"}. Executing controller action with signature Void Post(System.String) on controller test.Controllers.ValuesController (test).
[14:02:03 DBG 7] Execution plan of authorization filters (in the following order): ["None"]
[14:02:03 DBG 7] Execution plan of resource filters (in the following order): ["Microsoft.AspNetCore.Mvc.ViewFeatures.Internal.SaveTempDataFilter"]
[14:02:03 DBG 7] Execution plan of action filters (in the following order): ["Microsoft.AspNetCore.Mvc.ModelBinding.UnsupportedContentTypeFilter (Order: -3000)", "Microsoft.AspNetCore.Mvc.Infrastructure.ModelStateInvalidFilter (Order: -2000)"]
[14:02:03 DBG 7] Execution plan of exception filters (in the following order): ["None"]
[14:02:03 DBG 7] Execution plan of result filters (in the following order): ["Microsoft.AspNetCore.Mvc.ViewFeatures.Internal.SaveTempDataFilter", "Microsoft.AspNetCore.Mvc.Infrastructure.ClientErrorResultFilter (Order: -2000)"]
[14:02:03 VRB 7] Resource Filter: Before executing OnResourceExecuting on filter Microsoft.AspNetCore.Mvc.ViewFeatures.Internal.SaveTempDataFilter.
[14:02:03 VRB 7] Resource Filter: After executing OnResourceExecuting on filter Microsoft.AspNetCore.Mvc.ViewFeatures.Internal.SaveTempDataFilter.
[14:02:03 DBG 7] Attempting to bind parameter 'value' of type 'System.String' ...
[14:02:03 DBG 7] Attempting to bind parameter 'value' of type 'System.String' using the name '' in request data ...
[14:02:03 DBG 7] Rejected input formatter 'Microsoft.AspNetCore.Mvc.Formatters.JsonPatchInputFormatter' for content type 'application/json; charset=utf-8'.
[14:02:03 DBG 7] Selected input formatter 'Microsoft.AspNetCore.Mvc.Formatters.JsonInputFormatter' for content type 'application/json; charset=utf-8'.
[14:02:03 DBG 7] Connection id "0HLN4KB7M6AHT", Request id "0HLN4KB7M6AHT:00000001": started reading request body.
[14:02:03 DBG 7] Connection id "0HLN4KB7M6AHT", Request id "0HLN4KB7M6AHT:00000001": done reading request body.
[14:02:03 DBG 7] JSON input formatter threw an exception.
Newtonsoft.Json.JsonReaderException: Unexpected character encountered while parsing value: {. Path '', line 1, position 1.
   at Newtonsoft.Json.JsonTextReader.ReadStringValue(ReadType readType)
   at Newtonsoft.Json.JsonTextReader.ReadAsString()
   at Newtonsoft.Json.JsonReader.ReadForType(JsonContract contract, Boolean hasConverter)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
[14:02:03 DBG 7] Done attempting to bind parameter 'value' of type 'System.String'.
[14:02:03 DBG 7] Done attempting to bind parameter 'value' of type 'System.String'.
[14:02:03 DBG 7] Attempting to validate the bound parameter 'value' of type 'System.String' ...
[14:02:03 DBG 7] Done attempting to validate the bound parameter 'value' of type 'System.String'.
[14:02:03 VRB 7] Action Filter: Before executing OnActionExecuting on filter Microsoft.AspNetCore.Mvc.ModelBinding.UnsupportedContentTypeFilter.
[14:02:03 VRB 7] Action Filter: After executing OnActionExecuting on filter Microsoft.AspNetCore.Mvc.ModelBinding.UnsupportedContentTypeFilter.
[14:02:03 VRB 7] Action Filter: Before executing OnActionExecuting on filter Microsoft.AspNetCore.Mvc.Infrastructure.ModelStateInvalidFilter.
[14:02:04 DBG 7] The request has model state errors, returning an error response.
[14:02:04 VRB 7] Action Filter: After executing OnActionExecuting on filter Microsoft.AspNetCore.Mvc.Infrastructure.ModelStateInvalidFilter.
[14:02:04 DBG 7] Request was short circuited at action filter 'Microsoft.AspNetCore.Mvc.Infrastructure.ModelStateInvalidFilter'.
[14:02:04 VRB 7] Action Filter: Before executing OnActionExecuted on filter Microsoft.AspNetCore.Mvc.ModelBinding.UnsupportedContentTypeFilter.
[14:02:04 VRB 7] Action Filter: After executing OnActionExecuted on filter Microsoft.AspNetCore.Mvc.ModelBinding.UnsupportedContentTypeFilter.
[14:02:04 VRB 7] Result Filter: Before executing OnResultExecuting on filter Microsoft.AspNetCore.Mvc.ViewFeatures.Internal.SaveTempDataFilter.
[14:02:04 VRB 7] Result Filter: After executing OnResultExecuting on filter Microsoft.AspNetCore.Mvc.ViewFeatures.Internal.SaveTempDataFilter.
[14:02:04 VRB 7] Result Filter: Before executing OnResultExecuting on filter Microsoft.AspNetCore.Mvc.Infrastructure.ClientErrorResultFilter.
[14:02:04 VRB 7] Result Filter: After executing OnResultExecuting on filter Microsoft.AspNetCore.Mvc.Infrastructure.ClientErrorResultFilter.
[14:02:04 VRB 7] Before executing action result Microsoft.AspNetCore.Mvc.BadRequestObjectResult.
[14:02:04 DBG 7] List of registered output formatters, in the following order: ["Microsoft.AspNetCore.Mvc.Formatters.HttpNoContentOutputFormatter", "Microsoft.AspNetCore.Mvc.Formatters.StringOutputFormatter", "Microsoft.AspNetCore.Mvc.Formatters.StreamOutputFormatter", "Microsoft.AspNetCore.Mvc.Formatters.JsonOutputFormatter"]
[14:02:04 DBG 7] No information found on request to perform content negotiation.
[14:02:04 DBG 7] Attempting to select the first output formatter in the output formatters list which supports a content type from the explicitly specified content types '["application/problem+json", "application/problem+xml"]'.
[14:02:04 DBG 7] Selected output formatter 'Microsoft.AspNetCore.Mvc.Formatters.JsonOutputFormatter' and content type 'application/problem+json' to write the response.
[14:02:04 INF 7] Executing ObjectResult, writing value of type 'Microsoft.AspNetCore.Mvc.ValidationProblemDetails'.
[14:02:04 VRB 7] After executing action result Microsoft.AspNetCore.Mvc.BadRequestObjectResult.
[14:02:04 VRB 7] Result Filter: Before executing OnResultExecuted on filter Microsoft.AspNetCore.Mvc.Infrastructure.ClientErrorResultFilter.
[14:02:04 VRB 7] Result Filter: After executing OnResultExecuted on filter Microsoft.AspNetCore.Mvc.Infrastructure.ClientErrorResultFilter.
[14:02:04 VRB 7] Result Filter: Before executing OnResultExecuted on filter Microsoft.AspNetCore.Mvc.ViewFeatures.Internal.SaveTempDataFilter.
[14:02:04 VRB 7] Result Filter: After executing OnResultExecuted on filter Microsoft.AspNetCore.Mvc.ViewFeatures.Internal.SaveTempDataFilter.
[14:02:04 VRB 7] Resource Filter: Before executing OnResourceExecuted on filter Microsoft.AspNetCore.Mvc.ViewFeatures.Internal.SaveTempDataFilter.
[14:02:04 VRB 7] Resource Filter: After executing OnResourceExecuted on filter Microsoft.AspNetCore.Mvc.ViewFeatures.Internal.SaveTempDataFilter.
[14:02:04 INF 7] Executed action test.Controllers.ValuesController.Post (test) in 453.5132ms
[14:02:04 INF 7] Executed endpoint 'test.Controllers.ValuesController.Post (test)'
[14:02:04 DBG 7] Connection id "0HLN4KB7M6AHT" completed keep alive response.
[14:02:04 INF 7] Request finished in 589.1984ms 400 application/problem+json; charset=utf-8

罪魁祸首似乎JsonInputFormatter是由于application/json内容类型而触发的。

问题:

  1. 为什么会这样?我希望它能够工作,因为它{}是有效的 json。
  2. 如果您在 Visual Studio 中的方法上放置一个断点,Post它将不会被命中。因此,您似乎甚至无法读取原始值并自己解析 json。鉴于我不控制Content-Type请求的标头,我如何绕过 JsonInputFormatter?

要重现这一点,您可以使用您最喜欢的工具,而不仅仅是 curl,它也可以用 Postman、Fiddler 和我假设其他人重现。

标签: c#asp.net.netasp.net-core.net-core

解决方案


推荐阅读