Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Web API Development with ASP.NET Core 8

You're reading from   Web API Development with ASP.NET Core 8 Learn techniques, patterns, and tools for building high-performance, robust, and scalable web APIs

Arrow left icon
Product type Paperback
Published in Apr 2024
Publisher Packt
ISBN-13 9781804610954
Length 804 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Xiaodi Yan Xiaodi Yan
Author Profile Icon Xiaodi Yan
Xiaodi Yan
Arrow right icon
View More author details
Toc

Table of Contents (20) Chapters Close

Preface 1. Chapter 1: Fundamentals of Web APIs 2. Chapter 2: Getting Started with ASP.NET Core Web APIs FREE CHAPTER 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

Creating a simple REST web API project

In this section, we will use the .NET command-line interface (.NET CLI) to create a basic web API project and see how it works.

The .NET CLI is a command-line tool that helps you to create, develop, build, run, and publish .NET applications. It is included in the .NET SDK.

You have multiple ways to run .NET CLI commands. The most common way is to run the command in the terminal window or command prompt. Also, you can run the command in VS Code directly. VS Code provides an integrated terminal that starts at the root of your workspace. To open the terminal in VS Code, you can do any one of the following:

  • Press Ctrl + ` (on Windows) or Command + ` (on macOS) to open the terminal
  • Use the View | Terminal menu item to open the terminal
  • From the Command Palette, use the View: Toggle Terminal command to open the terminal

In the terminal, navigate to a folder where you want to create the project, then create a web API project by running the following command:

dotnet new webapi -n MyFirstApi -controllers
cd MyFirstApi
code .

The preceding commands create a new web API project and open it in VS Code. dotnet new provides many options to create various types of projects, such as web APIs, console apps, class libraries, and so on.

There are some options we can use to specify the project:

  • -n|--name <OUTPUT_NAME>: The name for the created output. If not specified, the name of the current directory is used.
  • -o|--output <OUTPUT_PATH>: The output path for the created project. If not specified, the current directory is used.
  • -controllers|--use-controllers: Indicates whether to use controllers for actions. If not specified, the default value is false.
  • -minimal|--use-minimal-apis: Indicates whether to use minimal APIs. The default value is false, but the -controllers option will override the -minimal option. If neither -controllers nor -minimal is specified, the default value of the -controllers option, which is false, will be used, so a minimal API will be created.

Important note

Since .NET 6.0, ASP.NET Core 6.0 provides a new way to create web API projects, which is called minimal APIs. It is a simplified approach for building APIs without controllers. We will introduce minimal APIs later. For now, we will use the traditional way to create a web API project with controllers. So, we need to specify the --use-controllers option.

To learn more about the dotnet new command, check this page: https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-new. We will introduce more details on the dotnet command in the following sections.

When you use VS Code to open the project, the C# Dev Kit extension can create a solution file for you. This feature makes VS Code more friendly to C# developers. You can see the following structure in the Explorer view:

The reason is that VS 2022 will create a sln file for the project, but .NET CLI does not. When using VS Code to open the project, the C# DevKit will create the sln file. I think it's worth mentioning it here.

The C# Dev Kit extension provides a new feature, the solution explorer, which is located at the bottom. This feature is especially useful when working with multiple projects in one solution. You can drag and drop the SOLUTION EXPLORER to the top to make it more visible.

When you use VS Code to open the project, the C# Dev Kit extension can create a solution file for you. This feature makes VS Code more friendly to C# developers. You can see the following structure in the Explorer view:

Figure 2.2 – The solution explorer and the folder structure

Figure 2.2 – The solution explorer and the folder structure

Next, we can start to build and run the project.

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 $19.99/month. Cancel anytime