且构网

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

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

更新时间:2023-02-17 10:08:28

据我所见,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.