How to return a specific status code and no contents from Controller?

前端 未结 5 1000
谎友^
谎友^ 2020-11-28 02:29

I want the example controller below to return a status code 418 with no contents. Setting the status code is easy enough but then it seems like there is something that needs

5条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-28 03:09

    this.HttpContext.Response.StatusCode = 418; // I'm a teapot

    How to end the request?

    Try other solution, just:

    return StatusCode(418);
    


    You could use StatusCode(???) to return any HTTP status code.


    Also, you can use dedicated results:

    Success:

    • return Ok() ← Http status code 200
    • return Created() ← Http status code 201
    • return NoContent(); ← Http status code 204

    Client Error:

    • return BadRequest(); ← Http status code 400
    • return Unauthorized(); ← Http status code 401
    • return NotFound(); ← Http status code 404


    More details:

    • ControllerBase Class (Thanks @Technetium)
    • StatusCodes.cs (consts aviable in ASP.NET Core)
    • HTTP Status Codes on Wiki
    • HTTP Status Codes IANA

提交回复
热议问题