如何正确地在.NET Core 3.0 Preview 9中实现JsonPatch?

时间:2019-09-12 21:27:38

标签: asp.net .net-core .net-core-3.0 asp.net-core-3.0

我正在尝试在.NET Core 3.0 Preview 9 Web API上实现JsonPatch。

模型:

public class TestPatch
{
    public string TestPath { get; set; }
}

网络api端点:

[HttpPatch()]
public async Task<IActionResult> Update([FromBody] JsonPatchDocument<TestPatch> patch)
{
   ...........
   return Ok();
}

JSON有效负载:

[
    {
        "op" : "replace",
        "path" : "/testPath",
        "value" : "new value"
    }
]

通过邮递员使用PATCH,出现此错误:

{
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
"title": "One or more validation errors occurred.",
"status": 400,
"traceId": "|492c592-4f7de4d16a32b942.",
"errors": {
    "$": [
        "The JSON value could not be converted to Microsoft.AspNetCore.JsonPatch.JsonPatchDocument`1[Test.Models.TestPatch]. Path: $ | LineNumber: 0 | BytePositionInLine: 1."
    ]
}
}

这是邮递员的完整请求/响应

PATCH /api/helptemplates HTTP/1.1
Content-Type: application/json
User-Agent: PostmanRuntime/7.16.3
Accept: */*
Cache-Control: no-cache
Postman-Token: a41813ea-14db-4664-98fb-ee30511707bc
Host: localhost:5002
Accept-Encoding: gzip, deflate
Content-Length: 77
Connection: keep-alive
[
{
"op" : "replace",
"path" : "/testPath",
"value" : "new value"
}
]
HTTP/1.1 400 Bad Request
Date: Thu, 12 Sep 2019 21:13:08 GMT
Content-Type: application/problem+json; charset=utf-8
Server: Kestrel
Transfer-Encoding: chunked
{"type":"https://tools.ietf.org/html/rfc7231#section-6.5.1","title":"One or more validation errors occurred.","status":400,"traceId":"|492c593-4f7de4d16a32b942.","errors":{"$":["The JSON value could not be converted to Microsoft.AspNetCore.JsonPatch.JsonPatchDocument`1[Test.Models.TestPatch]. Path: $ | LineNumber: 0 | BytePositionInLine: 1."]}}

JsonPatch参考:

<PackageReference Include="Microsoft.AspNetCore.JsonPatch" Version="3.0.0-preview9.19424.4" />

我的代码有什么问题?

谢谢。

2 个答案:

答案 0 :(得分:4)

此答案适用于3.1,但我认为它也适用于3.0...。 asp.net core 3.x中的默认json解析器不如NewtonsoftJson完整,因此请在Microsoft实现功能之前使用它。

将此nuget包添加到您的项目中: Microsoft.AspNetCore.Mvc.NewtonsoftJson

然后将其using语句添加到startup.cs中:

using Newtonsoft.Json.Serialization;

... 然后更改您的ConfigureService()以将NewtonsoftJson格式化程序包含在startup.cs中:

public void ConfigureServices(IServiceCollection services)
{
    services.AddControllers(setupAction =>
    setupAction.ReturnHttpNotAcceptable = true
   ).AddXmlDataContractSerializerFormatters().AddNewtonsoftJson(setupAction =>
   setupAction.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver());
   //...
}

您可能还必须将“接受集”添加到application / json到您的请求中,以防止它们返回XML。

希望这会有所帮助。

答案 1 :(得分:3)

使用JsonPatch软件包启用了对Microsoft.AspNetCore.Mvc.NewtonsoftJson的支持。要启用此功能,应用程序必须:

  • 安装Microsoft.AspNetCore.Mvc.NewtonsoftJson NuGet软件包。

  • 更新项目的Startup.ConfigureServices方法以包括 致电AddNewtonsoftJson

services
    .AddControllers()
    .AddNewtonsoftJson();

AddNewtonsoftJson与MVC服务注册方法兼容:

  • AddRazorPages
  • AddControllersWithViews
  • AddControllers

但是,如果您使用的是 asp.net core 3.x ,则

AddNewtonsoftJson替换了用于格式化所有 JSON内容的基于System.Text.Json的输入和输出格式化程序。要使用JsonPatch添加对Newtonsoft.Json的支持,同时保持其他格式化程序不变,请按以下方式更新项目的Startup.ConfigureServices

public void ConfigureServices(IServiceCollection services)
{
    services.AddControllers(options =>
    {
        options.InputFormatters.Insert(0, GetJsonPatchInputFormatter());
    });
}

private static NewtonsoftJsonPatchInputFormatter GetJsonPatchInputFormatter()
{
    var builder = new ServiceCollection()
        .AddLogging()
        .AddMvc()
        .AddNewtonsoftJson()
        .Services.BuildServiceProvider();

    return builder
        .GetRequiredService<IOptions<MvcOptions>>()
        .Value
        .InputFormatters
        .OfType<NewtonsoftJsonPatchInputFormatter>()
        .First();
}

前面的代码要求引用Microsoft.AspNetCore.Mvc.NewtonsoftJson,下面的using语句:

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Formatters;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using System.Linq;

可以在此link

中找到有关上述内容的简要说明和文档。