Created
June 2, 2021 14:41
-
-
Save manoj-choudhari-git/bb810ea076e003677a748742c5383d07 to your computer and use it in GitHub Desktop.
.NET Core Web API - Returning IActionResult from Actions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Route("api/[controller]")] | |
[ApiController] | |
public class ValuesController : ControllerBase | |
{ | |
[HttpPut("{id}")] | |
public IActionResult Put([FromRoute] int id, [FromBody] SubjectSummary summary) | |
{ | |
if(!Exists(id)) | |
{ | |
// NotFound() method returns 404 status code | |
// NotFoundResult is returned from here. | |
return NotFound(); | |
} | |
// Not possible to return specific type | |
// return summary; | |
// Ok() method returns OkResult (200OK Status) | |
return Ok(new { id = id, summary = summary }); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment