我正在寻求一种在Azure函数中实现适当的HTTP路径的方法。我发现了正在检查每个属性的空的示例,并将其添加到实体中以进行补丁。我发现这不是理想的,而只是解决方法。我为HTTP触发函数(V2)找到的唯一签名是
public async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "HTTP VERB", Route = "")] HttpRequest req,
ILogger log)
{
}
我需要通过" jsonpatchDocument ",而客户端将能够通过下面的补丁文档,
public async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "PATCH", Route = "")] **JsonPatchDocument<Customer> patch**,
ILogger log)
{
}
PATCH /api/customer
[
{
"op": "replace",
"path": "/firstname",
"value": "Vijay"
},
{
"op": "replace",
"path": "/email",
"value": "example@example.com"
},
]
以便我可以使用" patch.applyto()"来路径属性。是否可以在Azure函数中执行?
我找到了解决方案。我无法通过" JSONPATCHDCUMEMER" 键入Azure函数,但我可以按照以下内容将请求主体解析为jsonpatchDocument,
FunctionName("PatchInstitute")]
public async Task<IActionResult> PatchInstitute(
[HttpTrigger(AuthorizationLevel.Anonymous, "patch", Route = ApiConstants.BaseRoute + "/institute/{instituteId}")] HttpRequest req,
ILogger log, [Inject]IInstituteValidator instituteValidator, int instituteId, [Inject] IInstituteProvider instituteProvider)
{
try
{
//get existing record with Id here
var instituteDto = instituteProvider.GetInstitueProfileById(instituteId);
using (StreamReader streamReader = new StreamReader(req.Body))
{
var requestBody = await streamReader.ReadToEndAsync();
//Deserialize bosy to strongly typed JsonPatchDocument
JsonPatchDocument<InstituteDto> jsonPatchDocument = JsonConvert.DeserializeObject<JsonPatchDocument<InstituteDto>>(requestBody);
//Apply patch here
jsonPatchDocument.ApplyTo(instituteDto);
//Apply the change
instituteProvider.UpdateInstitute(instituteDto);
}
return new OkResult();
}
catch (Exception ex)
{
log.LogError(ex.ToString());
// return Generic Exception here
}
}
和jsonpathdocument从客户端通过
[
{
"op": "replace",
"path": "/isActive",
"value": "true"
}
]
在这里,您可以将任何字段传递到更新(补丁)