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
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Mastering Minimal APIs in ASP.NET Core

You're reading from   Mastering Minimal APIs in ASP.NET Core Build, test, and prototype web APIs quickly using .NET and C#

Arrow left icon
Product type Paperback
Published in Oct 2022
Publisher Packt
ISBN-13 9781803237824
Length 240 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Authors (3):
Arrow left icon
Marco Minerva Marco Minerva
Author Profile Icon Marco Minerva
Marco Minerva
Emanuele Bartolesi Emanuele Bartolesi
Author Profile Icon Emanuele Bartolesi
Emanuele Bartolesi
Andrea Tosato Andrea Tosato
Author Profile Icon Andrea Tosato
Andrea Tosato
Arrow right icon
View More author details
Toc

Table of Contents (16) Chapters Close

Preface 1. Part 1: Introduction
2. Chapter 1: Introduction to Minimal APIs FREE CHAPTER 3. Chapter 2: Exploring Minimal APIs and Their Advantages 4. Chapter 3: Working with Minimal APIs 5. Part 2: What’s New in .NET 6?
6. Chapter 4: Dependency Injection in a Minimal API Project 7. Chapter 5: Using Logging to Identify Errors 8. Chapter 6: Exploring Validation and Mapping 9. Chapter 7: Integration with the Data Access Layer 10. Part 3: Advanced Development and Microservices Concepts
11. Chapter 8: Adding Authentication and Authorization 12. Chapter 9: Leveraging Globalization and Localization 13. Chapter 10: Evaluating and Benchmarking the Performance of Minimal APIs 14. Index 15. Other Books You May Enjoy

Creating a new minimal API project

Let’s start with our first project and try to analyze the new template for the minimal API approach when writing a RESTful API.

In this section, we will create our first minimal API project. We will start by using Visual Studio 2022 and then we will show how you can also create the project with Visual Studio Code and the .NET CLI.

Creating the project with Visual Studio 2022

Follow these steps to create a new project in Visual Studio 2022:

  1. Open Visual Studio 2022 and on the main screen, click on Create a new project:
Figure 1.1 – Visual Studio 2022 splash screen

Figure 1.1 – Visual Studio 2022 splash screen

  1. On the next screen, write API in the textbox at the top of the window and select the template called ASP.NET Core Web API:
Figure 1.2 – Create a new project screen

Figure 1.2 – Create a new project screen

  1. Next, on the Configure your new project screen, insert a name for the new project and select the root folder for your new solution:
    Figure 1.3 – Configure your new project screen

Figure 1.3 – Configure your new project screen

For this example we will use the name Chapter01, but you can choose any name that appeals to you.

  1. On the following Additional information screen, make sure to select .NET 6.0 (Long-term-support) from the Framework dropdown. And most important of all, uncheck the Use controllers (uncheck to use minimal APIs) option.
Figure 1.4 – Additional information screen

Figure 1.4 – Additional information screen

  1. Click Create and, after a few seconds, you will see the code of your new minimal API project.

Now we are going to show how to create the same project using Visual Studio Code and the .NET CLI.

Creating the project with Visual Studio Code

Creating a project with Visual Studio Code is easier and faster than with Visual Studio 2022 because you don’t have to use a UI or wizard, rather just a terminal and the .NET CLI.

You don’t need to install anything new for this because the .NET CLI is included with the .NET 6 installation (as in the previous versions of the .NET SDKs). Follow these steps to create a project using Visual Studio Code:

  1. Open your console, shell, or Bash terminal, and switch to your working directory.
  2. Use the following command to create a new Web API application:
    dotnet new webapi -minimal -o Chapter01

As you can see, we have inserted the -minimal parameter in the preceding command to use the minimal API project template instead of the ASP.NET Core template with the controllers.

  1. Now open the new project with Visual Studio Code using the following commands:
    cd Chapter01
    code.

Now that we know how to create a new minimal API project, we are going to have a quick look at the structure of this new template.

You have been reading a chapter from
Mastering Minimal APIs in ASP.NET Core
Published in: Oct 2022
Publisher: Packt
ISBN-13: 9781803237824
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
Banner background image