Instant download Coding Clean, Reliable, and Safe REST APIs with ASP.NET Core 8 1st Edition Anthony Giretti pdf all chapter
Instant download Coding Clean, Reliable, and Safe REST APIs with ASP.NET Core 8 1st Edition Anthony Giretti pdf all chapter
com
https://ebookmeta.com/product/coding-clean-reliable-and-
safe-rest-apis-with-asp-net-core-8-1st-edition-anthony-
giretti/
OR CLICK HERE
DOWLOAD NOW
https://ebookmeta.com/product/building-web-apis-with-asp-net-core-
meap-version-11-valerio-de-sanctis/
ebookmeta.com
https://ebookmeta.com/product/field-guide-to-clandestine-laboratory-
identification-and-investigation-2-ed-christian-donnell-r-jr/
ebookmeta.com
Birds Of Prey Hermann Huppen
https://ebookmeta.com/product/birds-of-prey-hermann-huppen/
ebookmeta.com
https://ebookmeta.com/product/zootechnologies-a-media-history-of-
swarm-research-1st-edition-sebastian-vehlken/
ebookmeta.com
https://ebookmeta.com/product/geometry-for-programmers-
meap-v11-oleksandr-kaleniuk/
ebookmeta.com
https://ebookmeta.com/product/given-bitten-and-bound-book-1-1st-
edition-amy-pennza/
ebookmeta.com
Handbook of the Irish Revival : An Anthology of Irish
Cultural and Political Writings 1891-1922 1st Edition
Declan Kiberd
https://ebookmeta.com/product/handbook-of-the-irish-revival-an-
anthology-of-irish-cultural-and-political-writings-1891-1922-1st-
edition-declan-kiberd/
ebookmeta.com
Coding Clean,
Reliable, and Safe
REST APIs with
ASP.NET Core 8
Develop Robust Minimal
APIs with .NET 8
Anthony Giretti
Coding Clean, Reliable, and Safe REST APIs with ASP.NET Core 8:
Develop Robust Minimal APIs with .NET 8
Anthony Giretti
La Salle, QC, Canada
Acknowledgments�����������������������������������������������������������������������������xiii
Prerequisites��������������������������������������������������������������������������������������xv
Introduction��������������������������������������������������������������������������������������xvii
iii
Table of Contents
iv
Table of Contents
Uploading Files��������������������������������������������������������������������������������������������155
Streaming Content��������������������������������������������������������������������������������������������169
Handling CORS��������������������������������������������������������������������������������������������������171
API Versioning���������������������������������������������������������������������������������������������������177
Versioning by Headers���������������������������������������������������������������������������������178
Versioning by Route�������������������������������������������������������������������������������������187
Documenting APIs���������������������������������������������������������������������������������������������190
Managing API Versions in Swagger�������������������������������������������������������������192
Adding Comments on Endpoints�����������������������������������������������������������������199
Grouping Endpoints by Tag��������������������������������������������������������������������������206
Other Customizations����������������������������������������������������������������������������������207
Summary����������������������������������������������������������������������������������������������������������212
v
Table of Contents
vi
Table of Contents
Index�������������������������������������������������������������������������������������������������419
vii
About the Author
Anthony Giretti is a senior developer/architect
at Marchex in Toronto, Canada. He appreciates
learning and teaching new technologies and
has a knack for web technologies (more than
17 years’ experience) and a keen interest in
.NET. His expertise in development and IT and
his passion for sharing his knowledge allow
him to deconstruct any web project in order
to help other developers achieve their project
goals. He loves to deal with performance constraints, high availability,
and optimization challenges. Anthony is the author of Beginning gRPC
with ASP.NET Core 6 (Apress), a six-time Microsoft MVP, and a Microsoft
Certified Software Developer (MCSD).
ix
About the Technical Reviewer
Fiodar Sazanavets is a Microsoft MVP and a
senior software engineer with over a decade
of professional experience. He primarily
specializes in .NET and Microsoft stack and
is enthusiastic about creating well-crafted
software that fully meets business needs.
He enjoys teaching aspiring developers and
sharing his knowledge with the community,
which he has done both as a volunteer and
commercially. Fiodar has created several
online courses, written a number of technical books, and authored other
types of educational content. He also provides live mentoring services,
both to groups and individuals. Throughout his career, he has built
software of various types and various levels of complexity in multiple
industries. This includes a passenger information management system
for a railway, distributed smart clusters of IoT devices, ecommerce
systems, financial transaction processing systems, and more. He has also
successfully led and mentored teams of software developers.
xi
Acknowledgments
Completing this book could not have been possible without the
participation and assistance of many people, and I would like to express
my special thanks to them. First, thanks to my wife, Nadege, who never
stopped supporting me. I love you!
Next, I would like to thank the rest of my family for their support.
This book has been written in special conditions since I was
hospitalized for a severe disease that could have taken my life. I haven’t
given up, and I hope this book will please you; if I have completed it, it’s for
a good reason, I hope!
I also would like to thank my colleagues at Marchex, especially
my friend (and colleague) Callon Campbell, who never stopped
encouraging me.
Thanks to my friend Dominique St-Amand, who has never been stingy
with comments to help me improve this book.
Last but not least, Fiodar Sazanavets! Thanks, my friend, for being part
of this journey; you were essential in this new challenge I set for myself.
Without you, I wouldn’t have succeeded.
xiii
Prerequisites
This book is aimed at beginner and intermediate developers who want to
take their Application Programming Interface (API) development skills to
the next level. In this book, I assume you know the basics of .NET, C#, and,
therefore, the fundamentals of Object-Oriented Programming (OOP). I
also assume you’ve already used Visual Studio and know how to use it. As
for web fundamentals, I’ve started from scratch, so if you don’t know much
about the Web, no problem!
xv
Introduction
Dear reader friend, welcome to this book!
In my career, I have worked in various companies and on various
complex APIs. Although each company had its challenges, I can assure you
that they all had one thing in common: their APIs lacked a lot of love and
care. They all suffered from the same problems: poor code organization
due to an accumulation of minor errors over the years, lack of consistency
in the definition of coding conventions, lack of technological refreshment,
misinterpretations of the HyperText Transfer Protocol (HTTP) and
Representational State Transfer (REST) principles, missing logging or bad
logging practice, and not enough care regarding performances.
I have always enjoyed helping teams overcome these difficulties,
and I have decided to write a book to share my experiences and guide
you through the best practices of API implementations. This book will
focus on some technical architecture of an API, but it will focus more on
coding practices to help you avoid the most common mistakes in your
development career. I will not cover solution architecture where an API
is built around other systems, but keep assured; I will show you how to
implement access to external data sources.
At the end of this book, you will know how to develop APIs with ASP.
NET Core 8 properly coded, performant, resilient, secure, testable, and
debuggable. You will go from a beginner/intermediate level to a senior
level by learning precisely WHAT you need to know without feeling
overwhelmed by a ton of information.
Let’s go!
xvii
CHAPTER 1
Introducing HTTP
and REST
Before we dive into ASP.NET Core 8 and API development, let’s first go
back to the basics of any web application. Whether a website is run from a
browser or a web service (web API), it’s always the same principle: a client
and a server will communicate together; a client will send a request to a
server, which will then respond to the client. This is all possible with the
magic of the HTTP communication protocol. Under this protocol, data
can be transported using different formats and constraints. Here is REST!
REST is an architectural concept of data representation. Of course, these
two should not be confused. In this chapter, we will cover the following
content:
• HTTP
Note In this book, I will often refer to RFCs. The reason is that I
want to teach you the good practices for using HTTP. However, in
practice, the actual implementation of those RFCs may differ. Finally,
while this chapter aims to teach you the good techniques with HTTP,
I will not cover all the HTTP capabilities. I’ll stick to what you need to
know about building clean APIs with ASP.NET Core.
There are also different versions of HTTP. HTTP has evolved. I will not
go into details; in the following, you can find the published versions of the
protocol:
• HTTP/0.9 (obsolete)
• HTTP/1.0 (obsolete)
2
Chapter 1 Introducing HTTP and REST
3
Chapter 1 Introducing HTTP and REST
These characteristics may seem abstract, but they will become more
apparent as we read this book together. In the next section, I will give
you an overview of HTTP requests and responses. This will help you
understand HTTP before going into detail.
4
Chapter 1 Introducing HTTP and REST
In the following section, I will detail the HTTP verbs, the request
headers, the format of a URI, the different parameters passed in a
request, the HTTP status codes, the response headers, and the payload
formats returned to the client. Once we finish those points, I will bonify
Figure 1-1 with more details.
HTTP Implementation
Let’s dive into more detail to see what HTTP verbs, request headers,
response headers, and HTTP status codes are and how the client passes its
parameters in HTTP requests combined with the invocation of a URI.
5
Chapter 1 Introducing HTTP and REST
HTTP Verbs
RFC 7231 defines the following verbs:
6
Chapter 1 Introducing HTTP and REST
7
Chapter 1 Introducing HTTP and REST
RFC 7231 does not describe all the existing verbs, and there are others!
RFC 5789 defines the PATCH verb. This RFC can be found here: www.rfc-
editor.org/rfc/rfc5789.html.
The PATCH verb can be confused with PUT and POST verbs because
they all allow modifying a resource on a server. PATCH partially updates a
resource (like POST) when PUT tends to replace a resource.
I see many developers confusing each other. Now you are aware of
what the RFCs indicate about these verbs, but see that it is commonly
accepted to use POST for resource creation or to replace GET verb when
there are too many parameters in the URI to put them in the body of a
POST. It’s also commonly accepted to use PUT to entirely or partially
replace a resource even if PATCH is made for that. Personally, I rarely
use PATCH, only when I want to update a single property of a resource
(e.g., a date). From the moment I start modifying and altering several
properties of a resource (a date, a status, a description, etc.), I instead
implement PUT.
If you recall, I briefly mentioned HTTP status codes in this section. The
following section will discuss how status codes link to HTTP verbs. Some
verbs are used essentially with certain HTTP statuses. In the next section,
I will list the HTTP statuses and what verbs they can be associated with.
8
Chapter 1 Introducing HTTP and REST
detail each HTTP status class and each HTTP code because RFC 7231
does a pretty good job of doing so, and I won’t use all of them in this book.
Regarding APIs, status codes are essential for clients to understand what
the server is trying to tell us. They provide us with insights on what to
do next.
An HTTP status code has three digits. The first digit defines the status
category, and there are five categories of HTTP status codes:
• 5xx: They tell the client that the request on the server
has not been completed due to an error.
RFC 7231 is not the only RFC that describes HTTP status codes.
However, it describes the codes most often used. RFC 4918 and RFC 6585
complete the list, with other codes covering other scenarios.
Table 1-1, taken from the following RFCs
9
Chapter 1 Introducing HTTP and REST
lists the association between HTTP status codes and HTTP verbs
commonly used as industry standards. I won’t use all of them in this book;
you will not need to know them by heart. On the other hand, knowing their
existence is valuable since you will know their existence and how to use
them when required. Later in this book, I’ll dig deeper into why I’m using
some of them in the code samples I provide.
Table 1-1. List of available HTTP status codes and verbs most often
used with them
Code Reason phrase RFC Associated verb
10
Chapter 1 Introducing HTTP and REST
11
Chapter 1 Introducing HTTP and REST
This may seem like a lot of HTTP status codes, but remember that in
99% of the cases, you will only use a handful of codes described here.
Later in this book, we will come back together to some of them, and I
will explain them to you with examples of their usefulness.
Now let’s move on to another essential component of an HTTP request
and response, the request and response headers.
12
Chapter 1 Introducing HTTP and REST
RFCs describe them in detail. Remember that this book will not cover
all possible use cases; specific headers are generated automatically by a
browser during the request, some during the response, and by the server.
You will not need to know them by heart. On the other hand, knowing they
exist is excellent as you get to know they exist and you can customize them
for your needs when necessary.
Note Although RFC 7231 describes (or redirects to other RFCs) the
best-known headers, in reality, there is a complete list of headers
(even the most unknown, but without many details) for which you can
consult RFC 4229 here: https://datatracker.ietf.org/doc/
html/rfc4229.
Request Headers
Like HTTP status codes, request headers are divided into classes, five
exactly:
• Controls headers
• Conditional headers
In the following subsections, I will tell you in what RFCs these headers
are described, and I will list the links of these RFCs at the end of this
section.
13
Chapter 1 Introducing HTTP and REST
Controls Headers
There are seven headers in the Controls class. Some of them have various
possible directives (key/value pair):
14
Chapter 1 Introducing HTTP and REST
Conditional Headers
15
Chapter 1 Introducing HTTP and REST
16
Chapter 1 Introducing HTTP and REST
17
Exploring the Variety of Random
Documents with Different Content
that noble author himself alleged that he was engaged in ‘giving
fame to the Queen,’ the latter, one morning, noticed the alleged fact
to Lord Hervey. The King was present, and his Majesty remarked:—‘I
dare say he will paint you in fine colours, the dirty liar.’ ‘Why not?’
asked Caroline; ‘good things come out of dirt sometimes. I have ate
very good asparagus raised out of dung?’ When it was said that not
only Lord Carteret, but that Lords Bolingbroke and Chesterfield were
also engaged in writing the history of their times, the Queen
critically anticipated ‘that all the three histories would be three heaps
of lies; but lies of very different kinds: she said Bolingbroke’s would
be great lies; Chesterfield’s little lies; and Carteret’s lies of both
31
sorts.’ It may be added, that where there were vice and
coarseness there was little respect for justice or for independence of
conduct. The placeman who voted according to his conscience,
when he found his conscience in antagonism against the court, was
invariably removed from his place.
In concluding this chapter, it may be stated that when Frederick
was about to bring forward the question of his revenue, the Queen
would fain have had an interview with the son she alternately
despised and feared, to persuade him against pursuing this measure
—the carrying out of which she dreaded as prejudicial to the King’s
health in his present enfeebled state. Caroline, however, would not
see her son, for the reason, as the mother alleged, that he was such
an incorrigible liar that he was capable of making any mendacious
report of the interview, even of her designing to murder him. She
had, in an interview with him, at the time of the agitation connected
with the Excise bill, been compelled to place the Princess Caroline,
concealed, within hearing, that she might be a witness in case of the
prince, her brother, misrepresenting what had really taken place.
When the King learned the prince’s intentions, he took the
matter much more coolly than the Queen. Several messengers,
however, passed between the principal parties, but nothing was
done in the way of turning the prince from his purpose. It was an
innocent purpose enough, indeed, as he represented it. The
parliament had entrusted to the King a certain annual sum for the
prince’s use. The King and Queen did not so understand it, and he
simply applied to parliament to solicit that august body to put an
interpretation on its own act.
The supposed debilitated condition of the King’s health gave
increased hopes to the prince’s party. The Queen, therefore, induced
him to hold levées and appear more frequently in public. His
improvement in health and good humour was a matter of
disappointment to those who wished him dying, and feared to see
him grow popular.
The animosity of the Queen and her daughter, Caroline, against
32
the Prince of Wales was ferocious. The mother cursed the day on
which she had borne the son who was for ever destroying her
peace, and would end, she said, by destroying her life. There was no
opprobrious epithet which she did not cast at him; and they who
surrounded the Queen and princess had the honour of daily hearing
them hope that God would strike the son and brother dead with
apoplexy. Such enmity seems incredible. The gentle Princess
Caroline’s gentlest name for her brother was ‘that nauseous beast;’
and in running over the catalogue of crimes of which she declared
him capable, if not actually guilty, she did not hesitate to say that he
was capable of murdering even those whom he caressed. Never was
family circle so cursed by dissension as this royal circle; in which the
parents hated the son, the son the parents; the parents deceived
one another, the husband betrayed the wife, the wife deluded the
husband, the children were at mutual antagonism, and truth was a
stranger to all.
CHAPTER VII.
THE BIRTH OF AN HEIRESS.