c# - 发送带有 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
解决方案
请注意,这是来自Microsoft.AspNetCore.Mvc
命名空间:
var result = new ObjectResult(new { error = "your error message here" })
{
StatusCode = 500
};
根据配置的格式化程序,它将序列化对象返回给客户端。对于 JSON(默认),它将返回以下内容:
{ "error" : "your error message here" }
推荐阅读
- c++ - 是否可以从对象创建引用视图?
- kubernetes - 如何连接 Kubernetes 中的 pod 以在外部服务器上点燃集群
- javascript - 如何从动态路由发送数据
- android - 随机处理改造 2 请求
- python - 如何在 AWS lambda python 中读取对象并转换为 json
- chmod - 在 webroot 之外,chmodded 到 777 的目录是否安全?
- amazon-advertising-api - 适用于品牌推广/商品推广的亚马逊广告 API - 是否可以获得超过 60 天的报告?
- python - 用 Python 实现 RSA 密码函数
- java - Spring Batch - StaxEventItemReader 处理 xml 中的无效行
- hex - 十六进制转换:诊断模式中的设备