ASP.NET Core 6 enables us to develop APIs that consist of a bare minimum of data, functions, and also dependences. Below’s how to check a marginal Internet API.
ASP.NET Core 6 presents a simplified hosting design that can be made use of to implement light-weight APIs by removing the need to compose the boilerplate code required in previous versions ASP.NET Core.
We went over exactly how to begin with minimal APIs, as well as just how to use logging and reliance injection with very little APIs, in previous short articles here as well as here. This post goes over just how we can check very little APIs in ASP.NET Core 6.
To deal with the code instances provided in this article, you ought to have Visual Workshop 2022 mounted in your system. If you don’t already have a copy, you can download Visual Studio 2022 below.
Produce an ASP.NET Core Web API project in Visual Studio 2022
First of all, allow’s produce an ASP.NET Core Web API project in Visual Studio 2022. Adhering to these actions will create a brand-new Internet API job in Visual Studio 2022:
- Introduce the Visual Studio 2022 IDE.
- Click “Create new task.”
- In the “Develop brand-new task” window, select “ASP.NET Core Internet API” from the listing of templates displayed.
- Click Next.
- In the “Configure your new task” home window, define the name and also area for the new task.
- Optionally examine the “Location service and also task in the exact same directory site” check box, depending upon your preferences.
- Click Next.
- In the “Additional Info” window shown next, uncheck the check box that claims “Usage controllers …” given that we’ll be using very little APIs in this example. Leave the “Verification Kind” as “None” (default).
- Guarantee that the check boxes “Enable Docker,” “Configure for HTTPS,” and also “Enable Open API Support” are unattended as we won’t be utilizing any of those functions here.
- Click Develop.
We’ll utilize this ASP.NET Core 6 Web API job to examine very little APIs in the subsequent areas of this post.
Remedy framework of the total minimal Internet API application
In this example, we’ll develop two applications, named MinimalAPIDemo and MinimalAPIDemoTest. MinimalAPIDemo is the marginal ASP.NET Core 6 Internet API that we’ll test, as well as MinimalAPITests is the test application. In our instance, MinimalAPITests will certainly contain one examination method to examine the MinimalAPIDemo API.
Produce a marginal Web API in ASP.NET Core 6
Let’s currently produce our marginal Internet API in ASP.NET Core 6. We’ll name it CustomerAPI. This Client API will certainly have the complying with documents:
- Customer (this stands for the version class).
- ICustomerRepository (this represents the customer repository interface).
- CustomerRepository (this represents the customer repository class that executes the ICustomerRepository user interface).
The Client model class.
Create a new file named Customer.cs and give it the following code.
namespace MinimalAPIDemo { public class Customer { public int Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public string Address { get; set; } public string City { get; set; } public string Country { get; set; } public string Phone { get; set; } } }
The ICustomerRepository interface
Now create a file named ICustomerRepository.cs and insert this code.
namespace MinimalAPIDemo { public interface ICustomerRepository { public Task<List<Customer>> GetCustomers(); } }
The CustomerRepository class
Next create a file named CustomerRepository.cs and insert the following code.
namespace MinimalAPIDemo { public class CustomerRepository : ICustomerRepository { private readonly List<Customer> _authors; public CustomerRepository() { _authors = new List<Customer> { new Customer { Id = 1, FirstName = "Joydip", LastName = "Kanjilal", Address = "ABC Hills", City = "Hyderabad", Country= "India", Phone = "0123456789" }, new Customer { Id = 2, FirstName = "Anand", LastName = "Narayanaswamy", Address = "XYZ Hills", City = "Thiruvananthapuram", Country= "India", Phone = "1234567890" }, new Customer { Id = 3, FirstName = "Charles", LastName = "Fisher", Address = "Dawson Road", City = "New York ", Country= "US", Phone = "1234567890" } }; } public async Task<List<Customer>> GetCustomers() { return await Task.FromResult(_authors); } } }
The Program.cs file
Write the following code in the Program.cs file to create the endpoint.
app.MapGet("/customers", async (ICustomerRepository customerRepository) => await customerRepository.GetCustomers());
For the sake of simplicity, we’ll create just one endpoint in this example. Add an instance of type ICustomerRepository as a scoped service as shown below.
builder.Services.AddScoped<ICustomerRepository, CustomerRepository>();
You should also add a partial class named Program. This is because the Program.cs file will be compiled into a private class Program, which cannot be accessed from outside of the assembly.
public partial class Program { }
This partial class will make the Program class accessible to any project that references this assembly. The complete source code of the Program.cs file is given below for your reference.
using MinimalAPIDemo; var builder = WebApplication.CreateBuilder(args); builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); builder.Services.AddScoped<ICustomerRepository, CustomerRepository>(); var app = builder.Build(); if (app.Environment.IsDevelopment()) { app.UseSwagger(); app.UseSwaggerUI(); } app.MapGet("/customers", async (ICustomerRepository customerRepository) => await customerRepository.GetCustomers()); app.Run(); public partial class Program { }
Create a marginal Web API examination job in ASP.NET Core 6.
Produce a Xunit test job named MinimalAPIDemo.Tests and rename the default device test file to MinimalAPITests.cs. Below is where you must compose your examination techniques. In this example, we’ll develop just one examination approach to test the endpoint we developed previously.
Now let’s create a test technique named GetAllCustomersTest with the following code.
[Fact] public async void GetAllCustomersTest() { await using var application = new WebApplicationFactory<Program>(); using var client = application.CreateClient(); var response = await client.GetAsync("/customers"); var data = await response.Content.ReadAsStringAsync(); Assert.Equal(HttpStatusCode.OK, response.StatusCode); }
The complete source code of the MinimalAPITests class is given below for your reference.
using Microsoft.AspNetCore.Mvc.Testing; using System.Net; using Xunit; namespace MinimalAPIDemo.Tests { public class MinimalAPITests { [Fact] public async void GetAllCustomersTest() { await using var application = new WebApplicationFactory<Program>(); using var client = application.CreateClient(); var response = await client.GetAsync("/customers"); var data = await response.Content.ReadAsStringAsync(); Assert.Equal(HttpStatusCode.OK, response.StatusCode); } } }
This being a minimal implementation, we have actually not used a data source or a data context, however merely an easy repository class with a few fixed data aspects– simply sufficient to illustrate how we can construct marginal APIs and also evaluate them in ASP.NET Core 6. I’ll have more to claim on marginal APIs in future posts here.