且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从 ASP.NET Core RC2 Web Api 返回 HTTP 500?

更新时间:2023-02-17 10:35:00

据我所知,ControllerBase 类中有辅助方法.只需使用 StatusCode 方法:

From what I can see there are helper methods inside the ControllerBase class. Just use the StatusCode method:

[HttpPost]
public IActionResult Post([FromBody] string something)
{    
    //...
    try
    {
        DoSomething();
    }
    catch(Exception e)
    {
         LogException(e);
         return StatusCode(500);
    }
}

您也可以使用 StatusCode(int statusCode, object value) 重载,它也可以协商内容.

You may also use the StatusCode(int statusCode, object value) overload which also negotiates the content.