Getting Started With Minimal API In .NET 7

Introduction

In this article, I am going to explain to you, how to create a minimal Web API in .NET 7.0 applications and explain the default project structure step by step.

Minimal Web APIs are architected to create HTTP APIs with minimal dependencies. They are ideal for microservices and apps that want to include only the minimum files, features, and dependencies in ASP.NET Core.

Read my previous articles, using the below links.

Prerequisites

  1. Install Visual Studio 2022 updated version 17.4.0
  2. Install .NET SDK 7.0

Connect To Visual Studio 2022 Community Edition and Create Your First Project

Step 1

First, install Visual Studio 2022 in your system.

Step 2

Go to all programs in your systems, we can see Visual Studio 2022 and Visual Studio Installer.

Getting Started With Minimal API In .NET 7

Step 3

Double-click on Visual Studio 2022 and it will open. It looks like the below screenshot. Opening it the first time it will take few time.

Getting Started With Minimal API In .NET 7

Getting Started With Minimal API In .NET 7

Creating Your First Project

Click on; create a new Project to create a new project.

Getting Started With Minimal API In .NET 7

You can see various project types there. Choose “ASP.Net Core Web API Application” project type for now.

Select Asp.Net Core Web API Application, and then click Next.

Getting Started With Minimal API In .NET 7

Give a valid name to your project and select a path for it. Then click the Next button.

Getting Started With Minimal API In .NET 7

Now, Select framework .NET 7.0(Standard Term Support). You can select the Authentication Type as None, Individual Account, Microsoft Identity platform, or windows. For the demo purpose, I am selecting none. Based on your project need and Authentication requirement you can select this option. You can select Configure for HTTPS based on your need. If you need Docker in your project then Select Enable Docker.

Select Use Controllers (uncheck to use minimal APIs) and Select Enable OpenAPI support.

Uncheck Do not use top-level statements.

Then click the create button.

 

Getting Started With Minimal API In .NET 7

 Asp.Net Core 7.0 Web API application created and project structure is shown below,

Getting Started With Minimal API In .NET 7

Let’s start exploring each folder and file that exists in the Project Structure for easy understanding.

Project Folder Structure Description

The details of the default project structure can be seen in the solution explorer, it displays all the projects related to a single solution.

.csproj File

Double-click on the project name in Solution Explorer to open .csproj file in the editor or Right-click on the project and then click on Edit Project File in order to edit the .csproj file. As shown in the following image.

Getting Started With Minimal API In .NET 7

Once clicked on Edit Project File, .csproj file will be opened in Visual Studio as shown below. Where you can see the target Framework net7.0, by default Nullanle and ImplicitUsings are enabled.

Getting Started With Minimal API In .NET 7

Connected Services

It contains details about all the service references added to the project. A new service can be added here, for example, if you want to add access to Cloud Storage of Azure Storage you can add the service here. As shown in the following image.

Getting Started With Minimal API In .NET 7

Dependencies

The Dependencies node contains all the references of the NuGet packages used in the project. Here the Frameworks node contains reference two most important dotnet core runtime and asp.net core runtime libraries. Project contains all the installed server-side NuGet packages, as shown below.

Getting Started With Minimal API In .NET 7

Properties

Properties folder contains a launchSettings.json file, which contains all the information required to lunch the application. Configuration details about what action to perform when the application is executed and contains details like IIS settings, application URLs, authentication, SSL port details, etc.

Getting Started With Minimal API In .NET 7

Getting Started With Minimal API In .NET 7

Controllers

Web API Controller handles incoming HTTP requests and sends response back to the caller.

Web API controller is a class that can be created under the Controllers folder. It can be derived from ControllerBase class.

Getting Started With Minimal API In .NET 7

Getting Started With Minimal API In .NET 7

appsettings.json

This file contains the application settings, for example, configuration details like logging details, database connection details.

Getting Started With Minimal API In .NET 7

Getting Started With Minimal API In .NET 7

Program.cs

It contains Main method.

Getting Started With Minimal API In .NET 7

WeatherForecast.cs

It’s the model class.

Getting Started With Minimal API In .NET 7

Now, build and Press Ctrl+F5 to run without the debugger. Visual Studio displays the following dialog: 

Getting Started With Minimal API In .NET 7

Select Yes if you trust the IIS Express SSL certificate.

Again, The following dialog is displayed:

Getting Started With Minimal API In .NET 7

Select Yes if you agree to trust the development certificate.

Output

Getting Started With Minimal API In .NET 7

Getting Started With Minimal API In .NET 7

You can see in the above image that Minimal API comes with a default weather forecast and Swagger Documentation for ASP.NET Core Web API with Controller.

Up to now we have only created and explored the default Minimal API and default project structure.

Conclusion

In this article we explained, how to create a minimal Web API in .NET 7.0 applications and explain default project structure step by step. I hope this article is useful to understand.