parent
0c058e81c0
commit
eb6d10749e
File diff suppressed because one or more lines are too long
@ -1,69 +1,65 @@
|
|||||||
# HTTP Responses & Status Codes
|
---
|
||||||
|
marp: true
|
||||||
|
paginate: true
|
||||||
|
math: mathjax
|
||||||
|
theme: buutti
|
||||||
|
title: 2. HTTP Responses
|
||||||
|
---
|
||||||
|
|
||||||
## Check http request format from frontend-basics
|
# HTTP Responses
|
||||||
|
|
||||||
# ASP.NET Status Codes
|
<!-- headingDivider: 5 -->
|
||||||
|
<!-- class: invert -->
|
||||||
|
|
||||||
|
## ASP.NET Status Codes
|
||||||
|
|
||||||
|
* For a primer on HTTP status codes, check [Frontend basics: Lecture 2](https://gitea.buutti.com/education/frontend-basics/src/branch/main/2-http.md#http-status-codes)
|
||||||
|
|
||||||
* There are multiple possible status codes for each action
|
* There are multiple possible status codes for each action
|
||||||
* The ControllerBase which the controllers should inherit from includes __result methods__ for creating ActionResult objects
|
* The `ControllerBase` the controllers should inherit from includes *__result methods__* for creating `ActionResult` objects
|
||||||
* All ActionResult objects implement IActionResult
|
* All `ActionResult` objects implement the `IActionResult` interface
|
||||||
* Includes at least a status code, and can contain data such as view items or an error message
|
* Includes at least a status code, and can contain data such as view items or an error message
|
||||||
* This result is processed into a response to then send to client
|
* This result is processed into a response to then sent to client
|
||||||
|
|
||||||
# ASP.NET Result Methods
|
### Result methods
|
||||||
|
|
||||||
| __Status Code__ | __Result Method__ | __Use__ |
|
| Status Code | Result Method | Usage |
|
||||||
| :-: | :-: | :-: |
|
|:-------------------|:-----------------|:----------------------------|
|
||||||
| __200 - OK__ | __Ok()_ | __GET, DELETE__ |
|
| `200 - OK` | `Ok()` | `GET`, `DELETE` |
|
||||||
| __201 - Created__ | __Created()_ | __POST__ |
|
| `201 - Created` | `Created()` | `POST` |
|
||||||
| __204 - No content__ | __NoContent()_ | __PUT, PATCH__ |
|
| `204 - No content` | `NoContent()` | `PUT`, `PATCH` |
|
||||||
| __400 - Bad request__ | __BadRequest()_ | __POST, PUT, PATCH__ |
|
| `400 - Bad request` | `BadRequest()` | `POST`, `PUT`, `PATCH` |
|
||||||
| __404 - Not found__ | __NotFound()_ | __All actions__ |
|
| `404 - Not found` | `NotFound()` | `All actions` |
|
||||||
| __Custom__ | __StatusCode()_ | __All actions__ |
|
| `Custom` | `StatusCode()` | `All actions` |
|
||||||
|
|
||||||
# ASP.NET Status Codes (continued)
|
|
||||||
|
|
||||||
[HttpGet("{id}")]
|
### Status code example
|
||||||
|
|
||||||
|
```csharp
|
||||||
|
[HttpGet("{id}")]
|
||||||
public IActionResult GetContactById(int id)
|
public IActionResult GetContactById(int id)
|
||||||
|
|
||||||
{
|
{
|
||||||
|
|
||||||
// Contacts = list of contact objects, fetched from some repository
|
// Contacts = list of contact objects, fetched from some repository
|
||||||
|
|
||||||
var contact = Contacts.FirstOrDefault(c => c.Id == id);
|
var contact = Contacts.FirstOrDefault(c => c.Id == id);
|
||||||
|
|
||||||
if (contact == null)
|
if (contact == null)
|
||||||
|
|
||||||
{
|
{
|
||||||
|
|
||||||
return NotFound();
|
return NotFound();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return Ok(contact);
|
return Ok(contact);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
```
|
||||||
|
---
|
||||||
|
|
||||||
The previous code first attempts to find a Contacts object using the ID provided in the URI parameter
|
* The previous code first attempts to find a `Contacts` object using the ID provided in the URI parameter
|
||||||
|
* If an item is not found, a 404 response is returned using `NotFound()`
|
||||||
If an item is not found, a 404 response is returned using NotFound()
|
 ➡ 
|
||||||
|
* Otherwise, the object is sent as a payload with a 200 OK code response using `Ok(contact)`
|
||||||
Otherwise, the object is sent as a payload with a 200 OK code response using Ok(contact)
|
 ➡ 
|
||||||
|
|
||||||

|
|
||||||
|
|
||||||

|
|
||||||
|
|
||||||

|
|
||||||
|
|
||||||

|
|
||||||
|
|
||||||
# Exercise 1: Returning Status Codes
|
|
||||||
|
|
||||||
Without using parameter constraints, modify your number list method from Lecture 2, Exercise 2 to return a status code 400 (Bad Request) if _k_ is smaller than one or larger than 100.
|
|
||||||
|
|
||||||
Add a helpful error message to the result for both cases. The message should be seen as a message body in the response.
|
## Exercise 1: Returning status codes
|
||||||
|
<!--_class: "exercise invert" -->
|
||||||
|
|
||||||
Test with Swagger/Postman.
|
1) Without using parameter constraints, modify your number list method from [Lecture 1, Exercise 3](1-aspnet-introduction#exercise-3-returning-your-own-list) to return a status code `400 (Bad Request)` if $k$ is smaller than $1$ or larger than $100$.
|
||||||
|
2) Add a helpful error message to the result for both cases. The message should be seen as a message body in the response.
|
||||||
|
3) Test with Swagger/Postman.
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue