Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Web API Development with ASP.NET Core 8

You're reading from  Web API Development with ASP.NET Core 8

Product type Book
Published in Apr 2024
Publisher Packt
ISBN-13 9781804610954
Pages 804 pages
Edition 1st Edition
Languages
Author (1):
Xiaodi Yan Xiaodi Yan
Profile icon Xiaodi Yan

Table of Contents (20) Chapters

Preface 1. Chapter 1: Fundamentals of Web APIs 2. Chapter 2: Getting Started with ASP.NET Core Web APIs 3. Chapter 3: ASP.NET Core Fundamentals (Part 1) 4. Chapter 4: ASP.NET Core Fundamentals (Part 2) 5. Chapter 5: Data Access in ASP.NET Core (Part 1: Entity Framework Core Fundamentals) 6. Chapter 6: Data Access in ASP.NET Core (Part 2 – Entity Relationships) 7. Chapter 7: Data Access in ASP.NET Core (Part 3: Tips) 8. Chapter 8: Security and Identity in ASP.NET Core 9. Chapter 9: Testing in ASP.NET Core (Part 1 – Unit Testing) 10. Chapter 10: Testing in ASP.NET Core (Part 2 – Integration Testing) 11. Chapter 11: Getting Started with gRPC 12. Chapter 12: Getting Started with GraphQL 13. Chapter 13: Getting Started with SignalR 14. Chapter 14: CI/CD for ASP.NET Core Using Azure Pipelines and GitHub Actions 15. Chapter 15: ASP.NET Core Web API Common Practices 16. Chapter 16: Error Handling, Monitoring, and Observability 17. Chapter 17: Cloud-Native Patterns 18. Index 19. Other Books You May Enjoy

ASP.NET Core Fundamentals (Part 1)

In the previous chapter, we learned how to create a basic REST API using ASP.NET Core. ASP.NET Core provides a lot of features that make it easy to build web APIs.

In this chapter, we will be covering the following topics:

  • Routing
  • Configuration
  • Environments

Routing is used to map incoming requests to the corresponding controller actions. We will discuss how to use attribute routing to configure the routing for ASP.NET Core web APIs. Configuration is used to provide the initial settings for an application on its startup, such as database connection strings, API keys, and other settings. Configuration is often used with environments, such as development, staging, and production. At the conclusion of this chapter, you will have the skills to create RESTful routes for your ASP.NET Core web APIs and utilize the ASP.NET Core configuration framework to manage configurations for different environments.

Technical requirements

The code examples in this chapter can be found at https://github.com/PacktPublishing/Web-API-Development-with-ASP.NET-Core-8.

You can use Visual Studio 2022 or VS Code to open the solutions.

Routing

In Chapter 2, we introduced how to create a simple ASP.NET Core web API project using the default controller-based template. The project uses some attributes, such as [Route("api/controller")], [HttpGet], and so on, to map incoming requests to the corresponding controller actions. These attributes are used to configure the routing for the ASP.NET Core web API project.

Routing is a mechanism that monitors incoming requests and determines which action method is to be invoked for those requests. ASP.NET Core provides two types of routing: conventional routing and attribute routing. Conventional routing is typically used for ASP.NET Core MVC applications, while ASP.NET Core web APIs use attribute routing. In this section, we will discuss attribute routing in more detail.

You can download the RoutingDemo sample project from /samples/chapter3/RoutingDemo/ in the chapter's GitHub repository.

What is attribute routing?

Open the Program.cs file in the RoutingDemo...

Configuration

ASP.NET Core provides a comprehensive configuration framework that makes it easy to work with configuration settings. A configuration is considered a key-value pair. These configuration settings are stored in a variety of sources, such as JSON files, environment variables, and command-line arguments, or in the cloud, such as Azure Key Vault. In ASP.NET Core, these sources are referred to as configuration providers. Each configuration provider is responsible for loading configuration settings from a specific source.

ASP.NET Core supports a set of configuration providers, such as the following:

  • The file configuration provider, such as, appsettings.json
  • The User secrets
  • The environment variables configuration provider
  • The command-line configuration provider
  • The Azure App Configuration provider
  • The Azure Key Vault configuration provider

The configuration of ASP.NET Core is provided by the Microsoft.Extension.Configuration NuGet package...

Environments

In the previous section, we introduced how to read the configuration settings from various resources, including the appsettings.json file, user secrets, environment variables, and command-line arguments. In this section, we will discuss environments in more detail.

Run the following command to create a new ASP.NET Core web API project:

dotnet new webapi -n EnvironmentDemo -controllers

You can download the example project named EnvironmentDemo from the /samples/chapter3/EnvironmentsDemo folder in the chapter's GitHub repository.

We have mentioned the default ASP.NET Core web API template contains an appsettings.json file and an appsettings.Development.json file. When we run the application using dotnet run, the application runs in the Development environment. So the configuration settings in the appsettings.Development.json file override the configuration settings in the appsettings.json file.

Add the following section to the appsettings.Development.json...

Summary

In this chapter, we explored three important components of ASP.NET Core: routing, configuration, and environments. We discussed how to configure routing for an ASP.NET Core web API application and how to read parameters from the request. Additionally, we learned how to read configuration values from various sources, such as appsettings.json, environment variables, and command-line arguments. We also explored how to read configurations based on the environment, allowing us to enable different features for different environments.

In the next chapter, we will learn about two more essential components of ASP.NET Core: logging and middleware.

lock icon The rest of the chapter is locked
You have been reading a chapter from
Web API Development with ASP.NET Core 8
Published in: Apr 2024 Publisher: Packt ISBN-13: 9781804610954
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime}