.NET核心补丁JsonPatchDocument在本地工作,但不在服务器上工作

时间:2017-02-27 22:15:30

标签: asp.net json

我有一个非常简单的ASP.NET Core应用程序,可以在我的本地计算机上完美运行。 当我发布我的项目时,一切正常(GET,POST,DELETE)但不是部分更新(PATCH)。我使用JsonPatchDocument<T>。这是代码:

[HttpPatch("{id}")]
public IActionResult PartiallyUpdateNote(int id, [FromBody] JsonPatchDocument<NoteForUpdateDto> patches)
        {
            _log.LogInformation("Partial update of #{0}. Patches: {1}", id, patches);

            if (patches == null) // <------ THIS IS ALWAYS TRUE 
            {
                _log.LogError("Invalid patch format");
                return BadRequest("Invalid patch format?");
            }

这是我试图从邮递员发送的json:

  

PATCH http://tomasz.look24.net/api/notes/1
  标题:   内容类型=应用/ JSON
   原始:[{“op”:“替换”,“路径”:   “/ title”,“value”:“ne555e”}]

这是来自服务器的日志(单个补丁命中):

2017-02-27T22:46:46.1647691+01:00 0HL2VB6EI3TIB [INF] Connection id ""0HL2VB6EGP0A9"" bad request data: ""Unexpected end of request content"" (86f1a409)
Microsoft.AspNetCore.Server.Kestrel.BadHttpRequestException: Unexpected end of request content
   at Microsoft.AspNetCore.Server.Kestrel.Internal.Http.MessageBody.ForContentLength.<ReadAsyncAwaited>d__4.MoveNext()
2017-02-27T22:46:46.1647691+01:00 0HL2VB6EI3TIB [INF] Executing action method "notes.api.Controllers.NotesController.PartiallyUpdateNote (notes.api)" with arguments (["1", ""]) - ModelState is Invalid (ba7f4ac2)
2017-02-27T22:46:46.1647691+01:00 0HL2VB6EI3TIB [INF] Partial update of #1. Patches: null (30b5833a)
2017-02-27T22:46:46.1647691+01:00 0HL2VB6EI3TIB [ERR] Invalid patch format (b8690943)
2017-02-27T22:46:46.1647691+01:00 0HL2VB6EI3TIB [INF] Executing ObjectResult, writing value "Microsoft.AspNetCore.Mvc.ControllerContext". (4e968210)
2017-02-27T22:46:46.1647691+01:00 0HL2VB6EI3TIB [INF] Executed action "notes.api.Controllers.NotesController.PartiallyUpdateNote (notes.api)" in 119502.6086ms (afa2e885)
2017-02-27T22:46:46.1647691+01:00 0HL2VB6EI3TIB [INF] Request finished in 119503.2131ms 400 text/plain; charset=utf-8 (15c52c40)
2017-02-27T22:46:46.1647691+01:00  [INF] Connection id ""0HL2VB6EGP0A9"" bad request data: ""Unexpected end of request content"" (86f1a409)
Microsoft.AspNetCore.Server.Kestrel.BadHttpRequestException: Unexpected end of request content
   at Microsoft.AspNetCore.Server.Kestrel.Internal.Http.MessageBody.ForContentLength.ReadAsyncImplementation(ArraySegment`1 buffer, CancellationToken cancellationToken)
   at Microsoft.AspNetCore.Server.Kestrel.Internal.Http.MessageBody.Consume(CancellationToken cancellationToken)
   at Microsoft.AspNetCore.Server.Kestrel.Internal.Http.Frame`1.<RequestProcessingAsync>d__2.MoveNext()
2017-02-27T22:46:46.1647691+01:00  [INF] Connection id ""0HL2VB6EGP0A9"" communication error (46f6d175)
Microsoft.AspNetCore.Server.Kestrel.Internal.Networking.UvException: Error -4077 ECONNRESET connection reset by peer

我在2分钟后收到此日志并显示消息:

  

服务器错误

     

502 - Web服务器在充当a时收到无效响应   网关或代理服务器。您所在的页面存在问题   寻找,它无法显示。当Web服务器(同时   充当网关或代理)联系上游内容服务器,   它收到内容服务器的无效响应。

有什么不对?服务器配置中是否缺少某些内容? 当我尝试将数据作为字符串获取时:

public IActionResult PartiallyUpdateNote(int id, [FromBody] string patches)

服务器端的一切都很好。必须有PartiallyUpdateNote(我相信)总是返回null。

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。

当我将PATCH更改为POST

时,它可以正常工作
[HttpPost("{id}")]

我想知道为什么?