.NET Studies
1. Generating the template
MinimalAPI is a new request structure for REST APIs introduced in .NET 8 that helps us create REST APIs faster using a simple but efficient implementation.
This new structure helps build REST APIs in .NET more quickly for a microservices architecture, reducing the need for a heavier structure for simple processes.
Creating from the Terminal
-
Check your .NET version with the following command:
Checking the version in the Terminal dotnet --version -
Run the following command in your terminal:
Creating a MinimalAPI project template dotnet new web -n MinimalProject -
Enter the newly created template:
Entering the project directory cd MinimalProject
Creating from Visual Studio 2022
- Start Visual Studio without opening a project and click
Create a new project
- Select the
ASP.NET Core Emptyproject type
- Configure your project name and solution name
- Choose the .NET version, preferably version 8 or later
- It will be created automatically and show the
home pageand its files on the right
File organization
The dotnet new web command creates a template project with an example route, defined in the Program.cs file.
Inside the directory you will find the following structure:
DirectoryProperties/
- launchSettings.json
Directoryobj/
- …
- Program.cs
- MinimalProject.csproj
- appsettings.Development.json
- appsettings.json
A bit of information about each file:
- properties/launchSettings.json → Used in .NET Core to configure how the project starts during development (when we run the project in Visual Studio or with the
dotnet runcommand in the terminal). - obj/ → A directory generated automatically by .NET during the project build (
dotnet build). It stores temporary files used by the compiler and works as a cache to speed up future builds. It should be ignored by.gitignoreso it is not pushed to the remote repository, and it can be deleted because it is recreated every time you run a build. - Program.cs → The entry point of a .NET project, where the application starts running. This file defines how the application is initialized and holds all the methods and commands we use in our project.
- MinimalProject.csproj → A
*.csprojfile is an XML file that tells .NET which files are part of the project, lists the NuGet packages used (external dependencies), defines the project type, and holds the build settings, .NET version, directories, etc. - appSettings.json → This file stores application settings such as connection strings, API keys, logging configuration, and much more. We can define different settings for each environment we have (Prod, Dev, Testing, among others), which avoids using the same settings in test and production environments.
Default initial code
These are the template files that come with the application:
Configuration files
{ "$schema": "https://json.schemastore.org/launchsettings.json", "profiles": { "http": { "commandName": "Project", "dotnetRunMessages": true, "launchBrowser": true, "applicationUrl": "http://localhost:5020", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } }, "https": { "commandName": "Project", "dotnetRunMessages": true, "launchBrowser": true, "applicationUrl": "https://localhost:7179;http://localhost:5020", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } } }}{ "Logging": { "LogLevel": { "Default": "Information", "Microsoft.AspNetCore": "Warning" } }, "AllowedHosts": "*"}.NET files
var builder = WebApplication.CreateBuilder(args);var app = builder.Build();
app.MapGet("/", () => "Hello World!");
app.Run();<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup> <TargetFramework>net8.0</TargetFramework> <Nullable>enable</Nullable> <ImplicitUsings>enable</ImplicitUsings> </PropertyGroup>
</Project>