Book Image

Hands-On RESTful Web Services with ASP.NET Core 3

By : Samuele Resca
Book Image

Hands-On RESTful Web Services with ASP.NET Core 3

By: Samuele Resca

Overview of this book

In recent times, web services have evolved to play a prominent role in web development. Applications are now designed to be compatible with any device and platform, and web services help us keep their logic and UI separate. Given its simplicity and effectiveness in creating web services, the RESTful approach has gained popularity, and this book will help you build RESTful web services using ASP.NET Core. This REST book begins by introducing you to the basics of the REST philosophy, where you'll study the different stages of designing and implementing enterprise-grade RESTful web services. You'll also gain a thorough understanding of ASP.NET Core's middleware approach and learn how to customize it. The book will later guide you through improving API resilience, securing your service, and applying different design patterns and techniques to achieve a scalable web service. In addition to this, you'll learn advanced techniques for caching, monitoring, and logging, along with implementing unit and integration testing strategies. In later chapters, you will deploy your REST web services on Azure and document APIs using Swagger and external tools such as Postman. By the end of this book, you will have learned how to design RESTful web services confidently using ASP.NET Core with a focus on code testability and maintainability.
Table of Contents (26 chapters)
Free Chapter
1
Section 1: Getting Started
3
Section 2: Overview of ASP.NET Core
10
Section 3: Building a Real-World RESTful API
19
Section 4: Advanced Concepts for Building Services

Implementing request model validation

The Catalog.Domain project also owns the validation logic of our request models. In this section, we will see how to implement the request validation logic part, which will be also used by our controllers in order to validate the incoming data. Here, I usually rely on the FluentValidation package, which provides a really readable way to perform validation checks on every type of object and data structure.

In order to add the FluentValidation package to our Catalog.Domain project, we can execute the following commands in the project folder:

dotnet add package FluentValidation
dotnet add package FluentValidation.AspNetCore

The FluentValidation package exposes the AbstractValidation class, which can be extended to implement our custom validation criteria for a request model class:

//Requests/Item/Validators/AddItemRequestValidator.cs

using FluentValidation...