0

我在https://docs.microsoft.com/en-us/aspnet/core/web-api/jsonpatch?view=aspnetcore-6.0中看到JsonPatch了功能,但它适用于 MVC。当我在以下代码中执行此操作时:

Program.cs

WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
builder.Services.Configure<JsonOptions>(opt =>
{
    opt.SerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
    opt.SerializerOptions.Converters.Add(new DecimalJsonConverter());
});
builder.Services.AddControllersWithViews().AddNewtonsoftJson();
...

PlayerModule.cs

public class PlayerModule : ICarterModule
{
  public override void AddRoutes(IEndpointRouteBuilder app)
  {
    app.MapMethods("/api/portal/player/{id}", new string[] { "PATCH" }, PatchPlayer);
  }
}
...
[HttpPatch]
private async Task<IResult> PatchPlayer(int id, [FromBody] JsonPatchDocument<PlayerSchema> data) {
  ... // The data is always empty
}

哪里PlayerSchema是:

public class PlayerSchema {
  public int ID { get; set; }
  public string Password { get; set; }
  public string EMail { get; set; }
  ... and other properties
}

PATCH /api/portal/player/1用身体打电话:

{
  "eMail": "something@some.net"
}

并且JsonPatchDocument始终为空(不为空,只是 Count=0)。

此功能的最小 API 替代方案是什么,或者它应该与 MVC 一样工作?

4

0 回答 0