How to return HTTP 500 from ASP.NET Core RC2 Web Api?

前端 未结 8 2447
忘掉有多难
忘掉有多难 2020-12-04 17:01

Back in RC1, I would do this:

[HttpPost]
public IActionResult Post([FromBody]string something)
{    
    try{
        // ...
    }
    catch(Exception e)
            


        
8条回答
  •  情书的邮戳
    2020-12-04 17:44

    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);
        }
    }
    

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

提交回复
热议问题