Vij*_*han 2 c# rest azure azure-functions
我正在寻找一种在 Azure 函数中实现正确 HTTP 路径的方法。我找到了一些示例,这些示例正在检查每个属性的 null 并将其添加到 PATCH 的实体中。我发现这并不理想,而只是一种解决方法。我为 HTTP 触发函数 (v2) 找到的唯一签名是,
public async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "HTTP VERB", Route = "")] HttpRequest req,
ILogger log)
{
}
Run Code Online (Sandbox Code Playgroud)
而不是这个,我需要传递“ JsonPatchDocument ”,客户端将能够传递 PATCH 文档,如下所示,
public async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "PATCH", Route = "")] **JsonPatchDocument<Customer> patch**,
ILogger log)
{
}
Run Code Online (Sandbox Code Playgroud)
public async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "HTTP VERB", Route = "")] HttpRequest req,
ILogger log)
{
}
Run Code Online (Sandbox Code Playgroud)
这样我就可以使用“ patch.ApplyTo() ”来路径属性。是否可以在 azure 功能中进行?
我为此找到了解决方案。我无法将“JsonPatchDcument”类型传递给 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
}
}
Run Code Online (Sandbox Code Playgroud)
从客户端传递的 JsonPathDocument 如下所示,
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
}
}
Run Code Online (Sandbox Code Playgroud)
在这里你可以传递任何要更新的字段(补丁)
| 归档时间: |
|
| 查看次数: |
1351 次 |
| 最近记录: |