If you need an API endpoint that gives you some (static) test data, you can join the search for the most complicated solution. My current favourite in unnecessary complexity is to use MongoDB to serve a handful of JSON objects.
However, if you prefer a much lighter approach and already use .Net, I strongly recommend that you try Web API for this task. It only takes a few lines to get your data in a format that you can access from your application. And if you want to keep going on, you can add more functionality with ease.
Get some test data
Services like Generatedata.com (as described in this blog post) allow you to click together a test data sample without much effort. You can export the data as a JSON file and give it a suitable name, for example people.json
.
Create the minimal API
We can create a new project with the “ASP.NET Core Web API” template or by using the command line:
We then add our JSON file with the test data and replace the whole Program.cs with these 12 lines of code:
1 2 3 4 5 6 7 8 9 10 11 12 |
var builder = WebApplication.CreateBuilder(args); var app = builder.Build(); app.UseHttpsRedirection(); app.MapGet("/people", () => { var mimeType = "application/json; charset=utf-8"; var testData = File.ReadAllBytes("people.json"); return Results.File(testData, contentType: mimeType); }); app.Run(); |
After we started the application, we can point our browser to https://localhost:7155/people (the port may vary) and access our test data:
Conclusion
I would much rather write these 12 lines of code than to go through all the troubles of installing a database like MongoDB to serve a few objects. I hope this post gives you an idea for a simpler approach to access your test data.