简体   繁体   English

使用自定义状态码抛出异常

[英]throw an exception with custom statuscode

Can I change the StatusCode in a catch(System.Exception) ?我可以更改catch(System.Exception)中的StatusCode吗?

try
        {
            var result = await _query.GetAllAsync<PersonDto>(nameof(Person));
            if (result == null)
            {
                throw Exceptions.UnprocessableEntity();
            }
            
            return new Response<List<PersonDto>>

        }
        catch (System.Exception)
        {
        

If its http status codes you are looking for the following example can help.如果您正在寻找以下示例的 http 状态代码可以提供帮助。

public IActionResult AddNewSample()
    {
        try
        {
            //Does work
            return Ok(); //Status200OK
        }
        catch (System.Exception ex)
        {
            return BadRequest(ex.Message); //Status400BadRequest

            //return StatusCode(500); choosing a different status code
        }
    }

The following link can help to return the appropriate status codes以下链接可以帮助返回适当的状态代码

I hope this helps but it would be useful if you update your question to include more information.我希望这会有所帮助,但如果您更新您的问题以包含更多信息,它将很有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM