为什么我的 PATCH 请求在 ASP.NET 控制器中为空?
Why is my PATCH request empty in an ASP.NET Controller?
我有以下 ASP.net 核心控制器:
[ApiVersion(ApiConstants.Versions.V1)]
[Route(RouteConstants.ApiControllerPrefix + "/tenants/" + RouteConstants.TenantIdRegex + "/entities")]
public class EntityController
{
[HttpPatch]
[SwaggerOperation(OperationId = nameof(PatchEntity))]
[Route("{controlId:guid}", Name = nameof(PatchEntity))]
[SwaggerResponse(StatusCodes.Status204NoContent, "Result of the patch")]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[Consumes(MediaTypes.Application.JsonPatch)]
public async Task<IActionResult> PatchEntity(string tenantId, Guid entityId, JsonPatchDocument<EntityModel> entityPatches)
{
//
}
}
控制器允许我修补现有实体。这是模型:
[JsonObject(MemberSerialization.OptIn)]
public class EntityModel
{
[JsonProperty(PropertyName = "isAuthorized")]
public bool IsAuthorized { get; set; }
}
对于我的测试,我正在使用 postman
发送实体补丁。我选择了针对此 URL:
的动词 PATCH
http://localhost:5012/api/v1/tenants/tenant-id/entities/01111111-0D1C-44D6-ABC4-2C9961F94905
在 headers 中,我添加了 Content-Type
条目并将其设置为 application/json-patch+json
。
这里是请求的body:
[
{ "op": "replace", "path": "/isAuthorized", "value": "false" }
]
我启动了应用程序并在控制器上设置了一个断点。使用正确的租户 ID 和实体 ID 命中断点。然而,entityPatches
没有任何操作:
entityPatches.Operations.Count = 0
因此,无法更新目标 EntityModel
的 属性 IsAuthorized
。我希望 Operations
属性 有一个 replace
操作,如 HTTP 请求中所定义。
问题
为什么JsonPatchDocument
class的Operations
属性缺少HTTP请求body中定义的补丁操作?
您缺少 entityPatches
参数上的 FromBody
属性,例如:
public async Task<IActionResult> PatchEntity(
string tenantId,
Guid entityId,
[FromBody] JsonPatchDocument<EntityModel> entityPatches)
//^^^^^^^^^^ Add this
{
//snip
}
我有以下 ASP.net 核心控制器:
[ApiVersion(ApiConstants.Versions.V1)]
[Route(RouteConstants.ApiControllerPrefix + "/tenants/" + RouteConstants.TenantIdRegex + "/entities")]
public class EntityController
{
[HttpPatch]
[SwaggerOperation(OperationId = nameof(PatchEntity))]
[Route("{controlId:guid}", Name = nameof(PatchEntity))]
[SwaggerResponse(StatusCodes.Status204NoContent, "Result of the patch")]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[Consumes(MediaTypes.Application.JsonPatch)]
public async Task<IActionResult> PatchEntity(string tenantId, Guid entityId, JsonPatchDocument<EntityModel> entityPatches)
{
//
}
}
控制器允许我修补现有实体。这是模型:
[JsonObject(MemberSerialization.OptIn)]
public class EntityModel
{
[JsonProperty(PropertyName = "isAuthorized")]
public bool IsAuthorized { get; set; }
}
对于我的测试,我正在使用 postman
发送实体补丁。我选择了针对此 URL:
PATCH
http://localhost:5012/api/v1/tenants/tenant-id/entities/01111111-0D1C-44D6-ABC4-2C9961F94905
在 headers 中,我添加了 Content-Type
条目并将其设置为 application/json-patch+json
。
这里是请求的body:
[
{ "op": "replace", "path": "/isAuthorized", "value": "false" }
]
我启动了应用程序并在控制器上设置了一个断点。使用正确的租户 ID 和实体 ID 命中断点。然而,entityPatches
没有任何操作:
entityPatches.Operations.Count = 0
因此,无法更新目标 EntityModel
的 属性 IsAuthorized
。我希望 Operations
属性 有一个 replace
操作,如 HTTP 请求中所定义。
问题
为什么JsonPatchDocument
class的Operations
属性缺少HTTP请求body中定义的补丁操作?
您缺少 entityPatches
参数上的 FromBody
属性,例如:
public async Task<IActionResult> PatchEntity(
string tenantId,
Guid entityId,
[FromBody] JsonPatchDocument<EntityModel> entityPatches)
//^^^^^^^^^^ Add this
{
//snip
}