c# - 如何从 ASP.NET 5 Web Api 返回 HTTP 500?

标签 c# asp.net-web-api http-status-code-404 asp.net-core

Controller 现在具有用于 201 的函数 CreatedAtRoute(),用于 400 的 HttpBadRequest() 等。我没有看到一个用于 500 的函数,我认为它是 HttpInternalServerError()

不过,我可以创建并返回 HttpStatusCodeResult 类:

[HttpPost]
public IActionResult Post([FromBody]string something)
{    
   ...
    try{
    }
    catch(Exception e)
    {
         return new HttpStatusCodeResult((int)HttpStatusCode.InternalServerError);
    }
}

但我想从 e 返回一些信息。这对于现场来说可能是不好的做法,但是当我们进行测试时,我们希望看到 API 调用返回的正文中的错误。

HttpStatusCodeResult 没有 object 类型的属性或任何用于提供元数据的属性。

要做什么?我不想返回 400,因为这不是内部服务器错误的意思。

最佳答案

return ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "Message describing the error here"));

return InternalServerError(new Exception("SOME CUSTOM MESSAGE"));

关于c# - 如何从 ASP.NET 5 Web Api 返回 HTTP 500?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34206391/

相关文章:

c# - 在 C# 中,我可以根据使用的类型 (T) 使用泛型的特定实现吗

c# - "' System.IO.FileSystemInfo.FullPath C# ' is inaccessible due to its protection level"错误

php - 当我使用 jquery ajax 调用 php 文件时,wordpress 返回 404

javascript - Node.js 表达 POST 404ing

c# - 在C#中使用savefiledialog类保存excel文件

c# - 如何将 IList<MyObject> 作为参数传递给 Web API 中的 POST 方法?

c# - 如何在 Asp.net Web API 中使用特定的 CultureInfo

c# - 一种从 WebApi 中的 HttpRequestHeaders 中删除特定 cookie 的方法

php - CakePHP 重定向状态代码 404

c# - 如何在 C# 中安全地将 double 转换为小数