首页 > 解决方案 > 发送带有 actionresult 的消息

问题描述

我需要从 azure 函数返回服务器错误。

现在我使用InternalServerErrorResult(). 它只发送错误代码,不能使用此功能发送响应/消息。

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

当前实施

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

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

标签: c#exceptionerror-handlingazure-functions

解决方案


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

var result = new ObjectResult(new { error = "your error message here" })
{
    StatusCode = 500
};

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

{ "error" : "your error message here" }

推荐阅读