【发布时间】:2022-01-07 01:37:43
【问题描述】:
通常,我会在我的控制器操作中执行:
return Content(System.Net.HttpStatusCode.InternalServerError,
new MyCustomObject("An error has occurred processing your request.", // Custom object, serialised to JSON automatically by the web api service
ex.ToString()));`
但是Content 方法存在于控制器上。我做的 ExceptionHandler 有这个:
public override void Handle(ExceptionHandlerContext context)
{
context.Result = ???;
context.Result 的类型是IHttpActionResult,所以我需要做的是创建一个并将其粘贴在那里。我找不到任何可以让我在控制器之外创建IHttpActionResult 的构造函数或类似的东西。有什么简单的方法吗?
【问题讨论】:
-
你看到这个了吗:stackoverflow.com/questions/21758615/… - 尝试使用 ResponseMessage。
标签: c# asp.net-web-api