发送带有操作结果的消息

San*_*v S 4 c# error-handling exception azure-functions

我需要从天蓝色函数返回服务器错误。

现在我使用 实现相同的功能InternalServerErrorResult()。它仅发送错误代码,并且不能使用此函数发送响应/消息。

actionresult如何实现异常处理程序,其中可以使用azure 函数一起发送错误代码和消息

目前的实施

catch (Exception ex)
            {
                log.LogInformation("An error occured {0}" + ex);
                //json = new Response(ex.StackTrace, AppConstants.ErrorCodes.SystemException).SerializeToString();
                return (ActionResult)new InternalServerErrorResult();
            }
Run Code Online (Sandbox Code Playgroud)

这会在邮递员中返回空响应,错误为 500

Ara*_*yan 7

请注意,这是来自Microsoft.AspNetCore.Mvc命名空间:

var result = new ObjectResult(new { error = "your error message here" })
{
    StatusCode = 500
};
Run Code Online (Sandbox Code Playgroud)

根据配置的格式化程序,它将向客户端返回序列化对象。对于 JSON(默认),它将返回以下内容:

{ "error" : "your error message here" }
Run Code Online (Sandbox Code Playgroud)