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

前端 未结 8 2450
忘掉有多难
忘掉有多难 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:51

    For aspnetcore-3.1, you can also use Problem() like below;

    https://docs.microsoft.com/en-us/aspnet/core/web-api/handle-errors?view=aspnetcore-3.1

     [Route("/error-local-development")]
    public IActionResult ErrorLocalDevelopment(
        [FromServices] IWebHostEnvironment webHostEnvironment)
    {
        if (webHostEnvironment.EnvironmentName != "Development")
        {
            throw new InvalidOperationException(
                "This shouldn't be invoked in non-development environments.");
        }
    
        var context = HttpContext.Features.Get();
    
        return Problem(
            detail: context.Error.StackTrace,
            title: context.Error.Message);
    }
    

提交回复
热议问题