Reader small image

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

Product typeBook
Published inApr 2024
PublisherPackt
ISBN-139781804610954
Edition1st Edition
Concepts
Right arrow
Author (1)
Xiaodi Yan
Xiaodi Yan
author image
Xiaodi Yan

Xiaodi Yan is a seasoned software engineer with a proven track record in the IT industry. Since 2015, he has been awarded Microsoft MVP, showcasing his dedication to and expertise in .NET, AI, DevOps, and cloud computing. He is also a Microsoft Certified Trainer (MCT), Azure Solutions Architect Expert, and LinkedIn Learning instructor. Xiaodi often presents at conferences and user groups, leveraging his extensive experience to engage and inspire audiences. Based in Wellington, New Zealand, he spearheads the Wellington .NET User Group, fostering a vibrant community of like-minded professionals. Connect with Xiaodi on LinkedIn to stay updated on his latest insights.
Read more about Xiaodi Yan

Right arrow

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 2024Publisher: PacktISBN-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.
undefined
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 $15.99/month. Cancel anytime

Author (1)

author image
Xiaodi Yan

Xiaodi Yan is a seasoned software engineer with a proven track record in the IT industry. Since 2015, he has been awarded Microsoft MVP, showcasing his dedication to and expertise in .NET, AI, DevOps, and cloud computing. He is also a Microsoft Certified Trainer (MCT), Azure Solutions Architect Expert, and LinkedIn Learning instructor. Xiaodi often presents at conferences and user groups, leveraging his extensive experience to engage and inspire audiences. Based in Wellington, New Zealand, he spearheads the Wellington .NET User Group, fostering a vibrant community of like-minded professionals. Connect with Xiaodi on LinkedIn to stay updated on his latest insights.
Read more about Xiaodi Yan