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.
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);
Related videos on Youtube
Author by
Devsined
Updated on July 31, 2022Comments
-
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 notHttpResponseMessage
public IHttpActionResult Post(TaskBase model) { HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, model); response.Headers.Add("Id", model.Id.ToString()); return ResponseMessage(response); }
-
Felipe Oriani almost 10 yearsThere 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 calledRequest.CreateRespose()
method.
-
Devsined almost 10 yearsHow can I add add the Uri using my current API controller ?
-
Gildor almost 10 years@Devsined Is your controller inheriting from
ApiController
? -
Devsined almost 10 yearsYes 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 over 7 yearsOr, even better, CreatedAtRoute msdn.microsoft.com/en-us/library/…
-
Rafael Herscovici over 3 yearsSince 2016, did you encounter swagger and why this is not perfect for it?
-
chris31389 over 3 yearsI 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 about 3 yearsPerhaps "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.