Serve Test Data From ASP.NET Web API in .Net 8

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:

The template for ASP.NET Core Web API

We then add our JSON file with the test data and replace the whole Program.cs with these 12 lines of code:

After we started the application, we can point our browser to https://localhost:7155/people (the port may vary) and access our test data:

The test data is available in our API.

 

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.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.