使用.net core(azure)功能应用我想处理一个POST/command请求,在请求体中包含json对象。编写这类处理程序的最佳方法是什么?
我目前的解决方案是使用HttpTrigger从body读取json作为使用streamreader的流。
[FunctionName("command")]
public async Task<IActionAsynResult> HandleCommand(
[HttpTrigger(AuthorizationLevel.Anonymous, "POST", Route = "command")]
HttpRequest request,
ILogger logger)
{
using (StreamReader streamReader = new StreamReader(request.Body))
{
var requestBody = await streamReader.ReadToEndAsync();
var command = JsonConvert.DeserializeObject<Command>(requestBody);
}
return new OkObjectResult(new List<MyType>());
}
这是最好的方法吗?
如果您不需要自定义处理JSON序列化,您可以直接将Command
对象传递到您的Function中,而不是HttpRequest
。
[FunctionName("command")]
public async Task<IActionAsynResult> HandleCommand(
[HttpTrigger(AuthorizationLevel.Anonymous, "POST", Route = "command")]
Command command, ILogger logger)
{
return new OkObjectResult(new List<MyType>());
}
来源