c# - 发送带有操作结果的消息

标签 c# exception error-handling azure-functions

我需要从 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/

相关文章:

java - 我们应该为java中的每个try catch block 编写catch(Exception e)吗

c# - 我怎么可能在空对象上调用 HasValue?

windows - windows .BAT错误系统找不到指定的路径

ruby-on-rails - Rails 默认错误页面在开发时不显示

c++ - 如何在我的 C++ 程序中显示 Windows 的 "DLL not found"错误?

c# - 显示从 NuGet 下载的组件

c# - System.Web.Caching.Cache Dispose 对象是否从缓存中清除?

java - 两个数组的笛卡尔积

C# 数据适配器参数

c# - 从基类实例化派生类的通用工厂方法