我需要从 azure 函数返回服务器错误。
现在我使用 InternalServerErrorResult()
实现相同的功能。它只发送错误代码,不能使用此功能发送响应/消息。
如何使用 azure 函数中的 actionresult
实现错误代码和消息可以一起发送的异常处理程序
当前实现
catch (Exception ex)
{
log.LogInformation("An error occured {0}" + ex);
//json = new Response(ex.StackTrace, AppConstants.ErrorCodes.SystemException).SerializeToString();
return (ActionResult)new InternalServerErrorResult();
}
这在 postman 中返回一个空响应,错误为 500
最佳答案
请注意,这是来自 Microsoft.AspNetCore.Mvc
命名空间:
var result = new ObjectResult(new { error = "your error message here" })
{
StatusCode = 500
};
基于配置的格式化程序,它会将序列化的对象返回给客户端。 对于 JSON(默认),它将返回以下内容:
{ "error" : "your error message here" }
关于c# - 发送带有操作结果的消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55279516/