Better Programming

Advice for programmers.

Follow publication

Build a Placeholder API With the New ASP.NET Core Minimal API

And deploy it to Azure

Thomas Møller Jørgensen
Better Programming
Published in
4 min readFeb 7, 2022

--

Photo by Kelly Sikkema on Unsplash

You are starting a new project, building the next big thing on the internet. First, you build the client — a shining new Single Page Application. BUT… you need it to interact with an API to get the data. And you are not ready to build a full API yet.

There are many placeholder API’s out there, but they often have two problems:

  • The data does not match your data model.
  • The CRUD operations are not real (data is not really crud’ed).

I will show you how to build your own placeholder API in about the same time it takes to Google around to find something suitable. The API will use your own data model, and it will allow you to perform real CRUD operations where the changes are persisted — at least until you restart the API. And it is extensible so the data model can grow as your application grows.

You will need Visual Studio 2022 installed for this. It will use the ASP.NET Core Minimal API. And it will take 10 minutes or less (at least once you are done reading this article). The finished code can be found on GitHub.

Ready? Here we go!

Open Visual Studio 2022, and create a new project. Choose the ASP.NET Core Web API. We can name our new API anything, but that is a silly name so let us name it PostAPI.

Choose the ASP.NET Core Web API template

Make sure you uncheck the Use controllers (uncheck to use minimal API) checkbox.

Be sure to un-check the Use controllers (…) option to generate a minimal API

Once the project is created, open the Program.cs file and delete the WeatherForecast example api, but keep the general setup (including Swagger).

We also need to enable CORS, as we are probably going to access it from a browser client. Our Program file should look like this:

Now we add a json file to the project to hold our initial placeholder data; let us call it posts.json. Make sure it will be copied to the output folder. Then add the following data inside it:

Shameless self-promovation, I know.

Copy the JSON content, then place the cursor at the bottom of the Program file. We are going to let Visual Studio generate our data class: Go to Edit > Paste Special > Paste JSON as Classes. This will generate some classes for us.

We don’t need the RootObject, so delete it. Then rename Class1 to Post.

We load the data from posts.json into a local variable that we can then use in our endpoints (in just a moment). So add the following lines:

Finally, we implement the endpoints. In this example we add the five standard CRUD endpoints:

  • Get all posts (GET /posts)
  • Get by ID (GET /posts/{id})
  • Create a new post (POST /posts)
  • Update a post (PUT /posts/{id})
  • Delete a post (DELETE /posts/{id})

We want the Get by ID and Update to return NotFound if an unknown id is supplied, and otherwise all should be OK. Other APIs may have different needs. Anyway, add the following code between the initialization of posts and app.Run():

And Done! You can now run the application, and your API opens with a Swagger page. Try out the different endpoints.

Bonus: Deploy to Azure

You can create a free website / App Service in Azure. Then you can right-click your API project in Visual Studio and select Publish…. And now the rest of the team can access your amazing placeholder API.

Be aware that a free Azure App Service shuts down after 20minutes of inactivity. This resets the data, which after the initial load is only kept in memory. If you don’t want to wait 20 minutes — or you are a large team that continuously hits the API, thus keeping it alive — you may want to implement a /reset endpoint to reload the data. I will leave that as an exercise.

Alternatively, you can add it to a (non-free) standard-tier App Service Plan, turn on Keepalive, and then you can work with the same data for a longer period of time.

In short

Building a placeholder API with the new ASP.NET Core Minimal API is quite easy. You can do it in a matter of minutes, and you can adapt it to your application’s specific needs.

In an upcoming article, I will show how to use the ASP.NET Core Minimal API as a lean and flexible interface to applications of all sizes.

--

--

Thomas Møller Jørgensen
Thomas Møller Jørgensen

Written by Thomas Møller Jørgensen

Danish software developer. Fullstack. Tolkien fan. Kayaking. Does not drink coffee.

No responses yet

Write a response