Show me the Data!
Setting up ASP.NET Core Minimal API
Photo by Kieran White on Unsplash
In this article, I am going to add the GET, POST, and DELETE endpoints for API. When it says "minimal", it means it! Here's the code to add the endpoint for getting all users, getting a specific user by id, and creating a user.
So let's break this down.
New-ing up the DbContext
This line (line 26) won't be around forever. Eventually I will get the dbContext through dependency injection. But for now, I'm doing it this way.
Getting All Users
Lines 28-30 show the Get All endpoint. This being a minimal API means we don't need a separate controller for each endpoint. Just this. This (and the other endpoints) will get refactored later also. context here is basically a reference to the database itself. So context.Users gets me to the Users table. ToListAsync() queries the table and brings back the contents.
Getting a Specific User
Lines 32 - 39 show the endpoint for getting a particular User. The first parameter of MapGet on line 32 gives the place where the specific user id will be expected. The {userid: int} names the input "userid" and confirms it is an int. Here are a couple screenshots from Postman.
So I try to get user with userid = A. Obviously this won't work. I get a 404 error. If I look at the SwaggerUI, I will see the type error:
Adding a User
Lines 41-49 shows the POST endpoint for creating a new User. Here's how it looks in Postman.
Deleting a User
Here's the initial code for the Delete endpoint.
However, there was a problem. If in the same session I get one particular User and then go to delete the same User, I get the following error:
Recommended by LinkedIn
Basically what it is saying is:
Dude! I'm already tracking that one. Btw, you shouldn't re-use the DbContext for multiple calls.
So, when reading this Stack Overflow post, I see that not doing Dependency Injection has come to bite me. So, I'm gonna fix that.
(whistling Jeopardy tune)
Photo by Anthony Fomin on Unsplash
OK, and we're back.
Now I can successfully query a User (with the GET by id endpoint) and delete the same User right afterwards without the above error.
Dependency Injection - this won't hurt a bit
I changed the TwoDoTwoDoneDbContext class like so:
The connection string is no longer there. In fact, the whole OnConfiguring() is not there. Instead there is a constructor which expects a DbContextOptions<TwoDoTwoDoneDbContext> as a parameter.
Then I changed the Program.cs file like this:
Right before Bob does builder.Build(), I setup the DI. Also lines 32-34 show how this works. Instead of async () =>, the our TwoDoTwoDoneDbContext goes in there. Now this will be what used to be context. This pattern is repeated for each endpoint.
Next time I'm going to focus on a bit of the front-end with JWT authentication.
Previous Article: It's all about the data, base!
Next Article: You're my new best friend! (JWT Authentication)
Source Material:
Coding Clean, Reliable, and Safe REST APIs with ASP.NET Core 8: Develop Robust Minimal APIs with .NET 8 (Need O'Reilly subscription or Trial)