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

前端 未结 5 998
谎友^
谎友^ 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 02:57

    This code might work for non-.NET Core MVC controllers:

    this.HttpContext.Response.StatusCode = 418; // I'm a teapot
    return Json(new { status = "mer" }, JsonRequestBehavior.AllowGet);
    
    0 讨论(0)
  • 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
    0 讨论(0)
  • 2020-11-28 03:12

    If anyone wants to do this with a IHttpActionResult may be in a Web API project, Below might be helpful.

    // GET: api/Default/
    public IHttpActionResult Get()
    {
        //return Ok();//200
        //return StatusCode(HttpStatusCode.Accepted);//202
        //return BadRequest();//400
        //return InternalServerError();//500
        //return Unauthorized();//401
        return Ok();
    }
    
    0 讨论(0)
  • 2020-11-28 03:18

    The best way to do it is:

    return this.StatusCode(StatusCodes.Status418ImATeapot, "Error message");
    

    'StatusCodes' has every kind of return status and you can see all of them in this link https://httpstatuses.com/

    Once you choose your StatusCode, return it with a message.

    0 讨论(0)
  • 2020-11-28 03:18

    Look at how the current Object Results are created. Here is the BadRequestObjectResult. Just an extension of the ObjectResult with a value and StatusCode.

    https://github.com/aspnet/Mvc/blob/master/src/Microsoft.AspNetCore.Mvc.Core/BadRequestObjectResult.cs

    I created a TimeoutExceptionObjectResult just the same way for 408.

    /// <summary>
    /// An <see cref="ObjectResult"/> that when executed will produce a Request Timeout (408) response.
    /// </summary>
    [DefaultStatusCode(DefaultStatusCode)]
    public class TimeoutExceptionObjectResult : ObjectResult
    {
        private const int DefaultStatusCode = StatusCodes.Status408RequestTimeout;
    
        /// <summary>
        /// Creates a new <see cref="TimeoutExceptionObjectResult"/> instance.
        /// </summary>
        /// <param name="error">Contains the errors to be returned to the client.</param>
        public TimeoutExceptionObjectResult(object error)
            : base(error)
        {
            StatusCode = DefaultStatusCode;
        }
    }
    

    Client:

    if (ex is TimeoutException)
    {
        return new TimeoutExceptionObjectResult("The request timed out.");
    }
    
    0 讨论(0)
提交回复
热议问题