Testing Minimal APIs with .http files in Visual Studio 2022

ASP.NET Core's minimal APIs are an excellent tool for quickly and succinctly defining web APIs. Testing these APIs is equally important, and Visual Studio 2022's .http file editor offers a convenient way to do just that.

This article will guide you on how to create .http files for testing your minimal APIs, with a focus on syntax, request generation, and integration with the Endpoints Explorer in Visual Studio 2022.

Prerequisites

Visual Studio 2022 version 17.6 Preview 6 or later with the ASP.NET and web development workload installed.

Create a minimal API

Firstly, you need to create a minimal API in your ASP.NET Core project. Visual Studio 2022 and .NET 6 provide a straightforward way to create a minimal API. You can use the .NET CLI or Visual Studio 2022's New Project wizard to create a new ASP.NET Core Web API project.

Create an .http file

After you have created your minimal API, you can create an .http file to test the API endpoints:

  1. In Solution Explorer, right-click on the ASP.NET Core project.
  2. In the context menu, select Add > New Item.
  3. In the Add New Item dialog, select ASP.NET Core > General.
  4. Select HTTP File, and click on Add.

An .http file will be created in your project. You can now use this file to create HTTP requests to test your minimal API.

Add HTTP requests to the .http file

Next, add HTTP requests to the .http file to test your minimal API. Here's an example of a GET request:

                
                GET https://localhost:5001/YourMinimalAPIEndPoint HTTP/1.1
                
                

You can add as many HTTP requests as you want to the .http file, separated by lines with "###". For example:

                
                GET https://localhost:5001/YourMinimalAPIEndPoint HTTP/1.1
                ###

                POST https://localhost:5001/YourMinimalAPIEndPoint HTTP/1.1
                Content-Type: application/json

                {
                "Key": "Value"
                }
                ###
                
                

Use the Endpoints Explorer

The Endpoints Explorer in Visual Studio 2022 integrates with the .http file editor and provides a UI for testing HTTP requests. Here's how to use it:

  1. Enable the Endpoints Explorer by selecting Tools > Options > Environment > Preview Features > Web API Endpoints Explorer.
  2. Open the Endpoints Explorer by selecting View > Other Windows > Endpoints Explorer.
  3. Right-click on a request in the Endpoints Explorer and select Generate Request.

A request will be added to the .http file, and you can modify it if necessary.

Sending HTTP requests

Now that you've created an .http file and added HTTP requests, you can send them to test your minimal API:

  1. Make sure your API project is running.
  2. Open the .http file and locate the HTTP request you want to send.
  3. Click the green "run" button to the left of the request.

The response from your minimal API will appear in a separate pane on the right side of the editor window. You can inspect this response to verify that your API is functioning correctly.

In conclusion

.http files and the Endpoints Explorer provide powerful tools for testing minimal APIs in Visual Studio 2022. By using these tools, you can ensure your APIs are functioning as