Creating The Api and Returning Resources Slides PDF
Creating The Api and Returning Resources Slides PDF
Resources
Kevin Dockx
Architect
@KevinDockx https://www.kevindockx.com
Coming Up Clarifying the MVC pattern
Returning resources
Interacting with an API
Content negotiation
Getting a file
Model-View-Controller
An architectural software pattern for implementing user interfaces
Very common pattern
Clarifying the - Exists in many languages, supported by
many frameworks
MVC Pattern - Used to build client-facing ASP.NET Core
web applications
Model-View-Controller
An architectural software pattern for implementing user interfaces
Clarifying the MVC Pattern
Loose coupling
Separation of concerns
Testability
Reusability
Not a full system and/or application
Clarifying the architecture pattern!
MVC Pattern - Typically lives near the presentation layer
Clarifying the MVC Pattern
Model
View Controller
Clarifying the MVC Pattern
Model
Resource representation
(often JSON)
Demo
Routing app.UseEndpoints()
- Marks the position in the middleware
pipeline where the selected endpoint
is executed
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints => {
// map endpoints });
app.UseAuthorization();
app.UseEndpoints(endpoints => {
// map endpoints });
app.UseAuthorization();
app.UseEndpoints(endpoints => {
endpoints.MapControllers();});
Attribute-based Routing
No conventions are applied
This is the preferred approach for APIs
app.UseAuthorization();
app.MapControllers();
Attribute-based Routing
Shortcut: call MapControllers on the WebApplication object directly
- Default in .NET 6
- Mixes request pipeline setup with route management
Use attributes at controller and action level:
Attribute-based [Route], [HttpGet], …
Using Postman
Demo
Level 100
Informational
The Importance of Status Codes
200 – OK
201 – Created
204 – No Content
The Importance of Status Codes
409 - Conflict
Demo
Getting a file
Summary Model-View-Controller
- Model: application data logic
- View: display data
- Controller: interaction between View
and Model
The pattern improves reuse and testability
Summary
Routing matches a request URI to an action
on a controller
- Attribute-based routing is advised for APIs
Summary
Content negotiation is the process of
selecting the best representation for a
given response when there are multiple
representations available
Summary
Use the File method on ControllerBase
to return files
- Think about setting the correct
media type
Up Next:
Manipulating Resources and Validating Input