How can I code a Created-201 response using IHttpActionResult

47,996

Solution 1

If your view derives from ApiController, you should be able to call the Created method from base class to create such a response.

Sample:

[Route("")]
public async Task<IHttpActionResult> PostView(Guid taskId, [FromBody]View view)
{
    // ... Code here to save the view

    return Created(new Uri(Url.Link(ViewRouteName, new { taskId = taskId, id = view.Id })), view);
}

Solution 2

In ASP.NET Core, an IActionResult can be returned. This means you can return a ObjectResult with a 201 status code.

[HttpPost]
public async Task<IActionResult> PostAsync([FromBody] CreateModel createModel)
{
    // Create domain entity and persist
    var entity = await _service.Create(createModel);

    // Return 201
    return new ObjectResult(entity) { StatusCode = StatusCodes.Status201Created };
}

Solution 3

return Content(HttpStatusCode.Created, "Message");

Content is returning NegotiatedContentResult. NegotiatedContentResult is implements IHttpActionResult.

enter image description here

enter image description here

A similar problem: If you want to send NotFound with the message.

return Content(HttpStatusCode.NotFound, "Message");

Or:

return Content(HttpStatusCode.Created, Class object);
return Content(HttpStatusCode.NotFound, Class object);
Share:
47,996

Related videos on Youtube

Devsined
Author by

Devsined

Updated on July 31, 2022

Comments

  • Devsined
    Devsined almost 2 years

    How can I code a Created-201 response using IHttpActionResult ?

    IHttpActionResult has only these options

    • Ok
    • List item
    • NotFound
    • Exception
    • Unauthorized
    • BadRequest
    • Conflict Redirect
    • InvalidModelState

    What I am doing now is this code below, but I would like to use IHttpActionResult and not HttpResponseMessage

    public IHttpActionResult Post(TaskBase model)
    {
        HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, model);
        response.Headers.Add("Id", model.Id.ToString());
        return ResponseMessage(response);
    }
    
    • Felipe Oriani
      Felipe Oriani almost 10 years
      There is a implementation here strathweb.com/2013/06/… but I really doesn't like this. I prefer work with HttpResponseMessage which I can speficy all verbs and body just called Request.CreateRespose() method.
  • Devsined
    Devsined almost 10 years
    How can I add add the Uri using my current API controller ?
  • Gildor
    Gildor almost 10 years
    @Devsined Is your controller inheriting from ApiController?
  • Devsined
    Devsined almost 10 years
    Yes it is. I have the option of return Created. But I am having hard-time to return controller's Uri plus the id. I am return now just any Uri.
  • George Helyar
    George Helyar over 7 years
    Or, even better, CreatedAtRoute msdn.microsoft.com/en-us/library/…
  • Rafael Herscovici
    Rafael Herscovici over 3 years
    Since 2016, did you encounter swagger and why this is not perfect for it?
  • chris31389
    chris31389 over 3 years
    I think with swagger you can add attributes above the method which tell swagger what return statuses are possible and what the object shape will be
  • Jonas Stensved
    Jonas Stensved about 3 years
    Perhaps "should" is not the right word here. It "could". You can return actual models or throw exceptions also which is more elegant in many cases.