Testing REST APIs From Visual Studio 2022

Introduction

Today, we will look into a very useful feature introduced with Visual Studio 2022 version 17.5. In the past, we would test our APIs using tools like Swagger or, my favorite Postman. While these tools are very nice and easy to use, we can do the same directly from within Visual Studio using http/rest files. We will see how to do this today.

Creating the project and code 

I will create this application using Visual Studio 2022 Community Version 17.5.4 edition. 

Testing REST APIs from Visual Studio 2022

First, create an ASP.NET Core Web API project as below:

Testing REST APIs from Visual Studio 2022

Testing REST APIs from Visual Studio 2022

Testing REST APIs from Visual Studio 2022

Testing REST APIs from Visual Studio 2022

This will create the standard API project with the Weather Forecast controller. Next, add a new http file as below:

Testing REST APIs from Visual Studio 2022

Now update the code in the “WeatherForecastController.cs” file as below:

using Microsoft.AspNetCore.Mvc;

namespace WebAPITesting.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class WeatherForecastController : ControllerBase
    {
        private static readonly string[] Summaries = new[]
        {
        "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
    };

        private readonly ILogger<WeatherForecastController> _logger;

        public WeatherForecastController(ILogger<WeatherForecastController> logger)
        {
            _logger = logger;
        }

        [HttpGet(Name = "GetWeatherForecast")]
        public IEnumerable<WeatherForecast> Get()
        {
            return Enumerable.Range(1, 5).Select(index => new WeatherForecast
            {
                Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
                TemperatureC = Random.Shared.Next(-20, 55),
                Summary = Summaries[Random.Shared.Next(Summaries.Length)]
            })
            .ToArray();
        }

        [HttpPost("{id}", Name = "GetLocationDetails")]

        public IActionResult GetLocationDetails(int id)
        {
            return Ok(new LocationDetails
            {
                Id = id,
                Name = "Test Location",
                Address = "Test Address",
                City = "Test City",
                State = "Test State",
                ZipCode = "Test ZipCode"
            });
        }

        private class LocationDetails
        {
            public int Id { get; set; }
            public string? Name { get; set; }
            public string? Address { get; set; }
            public string? City { get; set; }
            public string? State { get; set; }
            public string? ZipCode { get; set; }
        }
    }
}

Finally, update the code in the “app.http” file as below:

GET https://localhost:7268/WeatherForecast

###

POST https://localhost:7268/WeatherForecast/1
Content-type: application/json

Now, when we run the application, we can simply click on the green arrow on the side of the calls in the app.http file and see the output directly inside Visual Studio.

Testing REST APIs from Visual Studio 2022

Testing REST APIs from Visual Studio 2022

The code is available at the below location:

https://github.com/munibrbutt/articles-code/tree/main/Using%20http%20or%20rest%20files%20in%20Visual%20Studio%202022

Summary

Today’s article looked at the new feature to test APIs from within the Visual Studio 2022 environment. This can help us to speed up the process of developing and testing APIs which form the interface backbone of micro-services.


Similar Articles