dotnet --version
2.2.300
创建一个空文件夹。在此文件夹中运行
dotnet new webapi
转到Startup.cs
并注释掉app.UseHttpsRedirection();
,然后运行:
dotnet run
等待该进程启动。假设您使用的是Windows 10的较新版本,并且运行Windows 10时开箱即用了curl.exe
:
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
内容类型而触发的。
问题:
{}
是有效的json。Post
方法上设置了一个断点,则不会被击中。因此,似乎您甚至都无法读取原始值并自己解析json。假设我不控制请求中的Content-Type
头,如何传递JsonInputFormatter?要重现此效果,您不仅可以使用卷曲工具,还可以使用自己喜欢的工具,Postman,Fiddler和我认为其他工具也可以重现。