* ***Attributes*** ([see C# Basics: Lecture 15](/education/csharp-basics/src/branch/main/15-design-patterns-in-csharp.md#attributes)) are a way of attaching metadata to entities (classes, methods, properties, etc.)
* In ASP.NET, attributes have a strong role in *__routing__*:
```csharp
[ApiController] // Attribute routing requirement,
// automatic HTTP 400 response, and more
[Route("[controller]")] // HTTP GET requests are routed to this method
[Route("[controller]")] // URIs with "/weatherforecast" are routed to this class
public class WeatherForecastController : ControllerBase
{
//...
}
// ...
[HttpGet] // URIs with "/weatherforecast" are routed to this class
[HttpGet] // HTTP GET requests are routed to this method
1) Change the routes in `WeatherForecastController.cs` so that the forecast result is printed at
@ -293,7 +293,7 @@ instead of
You can see the route change in the Swagger UI `GET` method.
## Handling HttpGet Requests
### Handling `HttpGet` Requests
* We have now established how to call methods with HTTP requests
* Additional parameters can be passed to the method with the URI:
@ -354,7 +354,7 @@ You can see the route change in the Swagger UI `GET` method.
</div>
## Exercise 3: Returning Your Own List
### Exercise 3: Returning Your Own List
<!--_class: "exercise invert" -->
1) Change the `GET` method so that instead of returning an `IEnumerable` of `WeatherForecast` objects, it returns a `List` of `string` objects.
@ -362,7 +362,9 @@ You can see the route change in the Swagger UI `GET` method.
2) Create a new method routed at `http://localhost:<port>/api/numberlist/k`, where `k` is any integer. The method should return an array of integers from `1` to `k`.
* For example, `http://localhost:<port>/api/numberlist/5` would return `[1,2,3,4,5]`. Test with browser (Swagger UI).
## Postman
## Sending requests with Postman
### Postman
* HTTP `POST` requests cannot be made with the browser's address bar, only `GET`!
* In websites, `POST` requests are usually made with forms
@ -376,14 +378,14 @@ You can see the route change in the Swagger UI `GET` method.
</div>
## Benefits of using Postman
### Benefits of using Postman
* When developing APIs, tools like Postman will almost always surface in the development cycle
* Postman lets you create configured HTTP requests to APIs, and save them to a JSON file
* This is great for testing your APIs without having to write code just for that purpose
* Supports all the necessary HTTP requests, like `GET`, `POST`, `PUT` and `DELETE`
## Exercise 4. Creating requests with Postman
### Exercise 4. Creating requests with Postman
<!--_class: "exercise invert" -->
Run the Weather API program, and test both methods with Postman.
* In the [previous Lecture 1](1-aspnet-introduction#an-example-controller-weathercontrollercs), we created a rudimentary ***controller-based web API*** in ASP.NET
* See also [Create web APIs with ASP.NET Core](https://learn.microsoft.com/en-us/aspnet/core/web-api/?view=aspnetcore-9.0)
* ***Note:*** It's also possible to create [minimal APIs](https://learn.microsoft.com/en-us/aspnet/core/tutorials/min-web-api?view=aspnetcore-9.0&tabs=visual-studio) without controllers in ASP.NET
* Let's delve deeper into controllers that return HTTP responses with specific ***HTTP 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` the controllers should inherit from includes *__result methods__* for creating `ActionResult` objects
### Controllers
* Controllers are classes that derive from the [`ControllerBase`](https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.controllerbase?view=aspnetcore-9.0) class
* In the project template, we had this controller:
```csharp
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
```
* `ControllerBase` provides many properties and methods for handling HTTP requests
* Inside the class, we can create methods that correspond to different endpoints (see [Lecture 1: Routing and endpoints](1-aspnet-introduction#routing-and-endpoints))
* These are called *__result methods__*
### Result methods
* Result methods return `ActionResult` objects
* For example, a `CreatedAtAction` returns the 201 status code
* 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 sent to client
* There are multiple possible status codes for each action (See the following table)