edit lecture 2 slides

- still missing controller theory
- readme updated
main
borb 1 month ago
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
* The ControllerBase which the controllers should inherit from includes __result methods__ for creating ActionResult objects
* All ActionResult objects implement IActionResult
* The `ControllerBase` the controllers should inherit from includes *__result methods__* for creating `ActionResult` objects
* 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
* 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__ |
| :-: | :-: | :-: |
| __200 - OK__ | __Ok()_ | __GET, DELETE__ |
| __201 - Created__ | __Created()_ | __POST__ |
| __204 - No content__ | __NoContent()_ | __PUT, PATCH__ |
| __400 - Bad request__ | __BadRequest()_ | __POST, PUT, PATCH__ |
| __404 - Not found__ | __NotFound()_ | __All actions__ |
| __Custom__ | __StatusCode()_ | __All actions__ |
| Status Code | Result Method | Usage |
|:-------------------|:-----------------|:----------------------------|
| `200 - OK` | `Ok()` | `GET`, `DELETE` |
| `201 - Created` | `Created()` | `POST` |
| `204 - No content` | `NoContent()` | `PUT`, `PATCH` |
| `400 - Bad request` | `BadRequest()` | `POST`, `PUT`, `PATCH` |
| `404 - Not found` | `NotFound()` | `All actions` |
| `Custom` | `StatusCode()` | `All actions` |
# ASP.NET Status Codes (continued)
[HttpGet("{id}")]
### Status code example
```csharp
[HttpGet("{id}")]
public IActionResult GetContactById(int id)
{
// Contacts = list of contact objects, fetched from some repository
var contact = Contacts.FirstOrDefault(c => c.Id == id);
if (contact == null)
{
return NotFound();
}
return Ok(contact);
}
```
---
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()
Otherwise, the object is sent as a payload with a 200 OK code response using Ok(contact)
![](imgs/3-http-responses-and-status-codes_0.png)
![](imgs/3-http-responses-and-status-codes_1.png)
![](imgs/3-http-responses-and-status-codes_2.png)
![](imgs/3-http-responses-and-status-codes_3.png)
# 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.
* 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()`
![w:700](imgs/3-http-responses-and-status-codes_0.png) ➡ ![](imgs/3-http-responses-and-status-codes_1.png)
* Otherwise, the object is sent as a payload with a 200 OK code response using `Ok(contact)`
![w:700](imgs/3-http-responses-and-status-codes_2.png) ➡ ![](imgs/3-http-responses-and-status-codes_3.png)
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.

@ -3,7 +3,7 @@
| # | Lecture | Slides |
|:--|:--------------------------------------------------------------------------|:---------------------------------------------------------------------------|
| 1 | [Introduction to ASP.NET](1-aspnet-introduction.md) | [Download slides](1-aspnet-introduction-slides.html?raw=1) |
| 3 | [HTTP Responses & Status Codes](3-http-responses-and-status-codes.md) | [Download slides](3-http-responses-and-status-codes-slides.html?raw=1) |
| 2 | [HTTP Responses](2-http-responses.md) | [Download slides](2-http-responses-slides.html?raw=1) |
| 4 | [MVC Pattern & Repositories](4-mvc-pattern-and-repositories.md) | [Download slides](4-mvc-pattern-and-repositories-slides.html?raw=1) |
| 5 | [5. REST Architecture](5-rest-architecture.md) | [Download slides](5-rest-architecture-slides.html?raw=1) |
| 6 | [Model validation & API Design](6-model-validation-and-designing-apis.md) | [Download slides](6-model-validation-and-designing-apis-slides.html?raw=1) |

Loading…
Cancel
Save