Create A Minimal Web API With ASP.NET Core And Publish It To Azure API Management With Visual Studio

Introduction to Minimal Web API

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

The 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 my case we have 6.0.101:

2. We will create our application,

dotnet new webapi -minimal -o sampleAPI

3. Open the folder created on Visual Studio Code, and we will see the file structure, as we can see the folder content is smaller than 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 keyword at the top of the window as below 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 to 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:

6. Let us now open Program.cs and clear everything from it so we can build a very simple API with less than 4 lines of code!

7. 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 select 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 existing Azure App Service if you have already created 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 API Management to check our API.