Test your APIs from within Visual Studio 2022
Introduction
Today, we will look into a very useful feature just 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 now 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 be creating this application using Visual Studio 2022 Community Version 17.5.4 edition.?
First create a ASP.NET Core Web API project as below:
This will create the standard API project with the Weather Forecast controller. Next, add a new http file as below:
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/WeatherForecas
###
POST https://localhost:7268/WeatherForecast/1
Content-type: application/jsont
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.
The code is available at the below location:
Summary
In today’s article, we 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.