Create a minimal web API with ASP.NET Core and publish it to Azure API Management with Visual Studio

Rebai Hamida
7 min readMar 1, 2022

--

Introduction to Minimal Web API

It is a new approach for building APIs without all the complex structure of MVC, so, according to the name: minimal, it include the essential components needed to build HTTP APIs. All what you need is only a csproj and a program.cs.

Benefit of using Minimal Web API

  • Less complex than before.
  • Easy to learn and use.
  • Don’t need MVC structure, no controllers!
  • Minimal code to build and compile the application which means the application runs much faster so more performance.
  • Latest improvements and functionalities of .NET 6 and C#10.

Prerequisites

  • dotnet 6 SDK
  • Visual Studio 2022 or Visual Studio Code (we will use both of them)

We will use two methods to create our Minimal Web API

Method 1: using .NET CLI

1- We will open PowerShell and we will check the .NET version as bellow

dotnet --version

The result should be 6, in our case we have 6.0.101:

2- we will create our application:

dotnet new webapi -minimal -o sampleAPI

3- Open the created folder on Visual Studio Code, and we will see the file structure, as we can see the folder content is smaller then Web APIs project as before, no more startup class or controller folders all we have is a Program.cs

4- Build and run the solution, inside our terminal, if you don’t have a terminal, open a new one. To do that, click on the Terminal Menu and then select the New Terminal option as shown in the below image.

Build ASP.NET Core Web API Project Using Visual Studio Code Terminal
Run the ASP.NET Core Web API project using .NET Core CLI

As you can see in the next image, our Web API Application is running on two different ports. They are as follows:

https://localhost:7025 and http://localhost:5272

But if we follow the link, we get a 404 error because we have to add Swagger to the link.

Method 2: using Visual Studio

1- We will open Visual Studio 2022, and we will click on ‘ Create a new project’

Visual Studio 2022

2- You can search for the template using API as a key work in the top of the window as bellow and select C# as a language to find easier the template of web API.

ASP.NET Core Web API template

3- It is time to configure our project, we have to add the project name, the location and we can modify the solution name.

4- In the next window, we have additional information, we will select the new framework .NET 6 in our case, the authentication type if needed but we will keep None, we can configure HTTPs if we want add security to our web application, if we Enable Docker to use containers we will select Docker OS (Linux or Windows), in our case we will not enable Docker, and we need to uncheck ‘Use Controller’ to use minimal APIs, we can enable OpenAPI support to use Swagger, I will disable it.

Minimal APIs

5- Let’s check the project structure:

5- Let us now Open Program.cs and clear everything from it so we can build a very simple API with less then 4 lines of code!

6- Let’s run our sample:

Let’s compare this project to another using controller!

Project Structure

Program.cs

As you can see, Minimal APIs are more simple with less code!

Publish Minimal APIs to Azure API Management with Visual Studio

1- Open Azure portal and create a new API Management, fill all information needed to create an API Management.

API management in Azure Portal

2- We go back to Visual Studio 2022 to publish our web API app, a right click on our Solution Explorer after right-click the project and select Publish:

3- We will publish the API app to Azure App Service first:

— We will select Azure in Publish window and we will click on the Next button:

— We will s elect Azure App Service (Linux) and we will click on the Next button:

— We will click on the + button to Create a new Azure App Service or you can select an inexistant Azure App Service if you already create it before on Azure Portal or using Azure CLI for example:

— When the Create App Service dialog appears, we will fill all information needed to create a new App Service like: the App Name, Resource Group, and App Service Plan entry fields. You can keep these names or change them.

— We will click on Finish and after the creation is completed, we will click on publish.

If you don’t have any API Management already created skip the next step and click on Finish to publish the application.

4- We will add our API to Azure API Management

— Let’s open API management Service instance created previously and we will click on APIs that is in the left of the window:

— We will delete Echo API and we will add a new API:

— Enter the following values in the Create a HTTP API dialog that appears:

  • Display Name: WeatherForecasts
  • Name: weatherforecasts
  • API Url suffix: v1
  • Leave the Web service URL field empty.

And we will have this result, no operation in our API because we have to publish the previous API to the Azure API Management.

5- Publish our previous web API to Azure API Management

— We Switch back to Visual Studio, to publish window, and we will select the previous published Azure App Service:

— Let’s switch back to the API Management to check our API.

--

--

Rebai Hamida
Rebai Hamida

Written by Rebai Hamida

Senior Cloud Application Architect, Microsoft MVP in Developer Technologies, MCT, Technical writer, Speaker