Show me the Data!

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.


Article content

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.


Article content
Shows a valid call


Article content
404 Error when putting in invalid content

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:


Article content
Swagger shows me the type error


Adding a User


Article content

Lines 41-49 shows the POST endpoint for creating a new User. Here's how it looks in Postman.

Article content

Deleting a User

Here's the initial code for the Delete endpoint.

Article content

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:

Article content

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)

Article content

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:

Article content

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:


Article content

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)

Deleting with Entity Framework Core

Dependency Injection for DbContext (MS Docs)

Minimal API (MS Docs)

To view or add a comment, sign in

More articles by Dave Richards

  • Lookin' at the Big Picture

    Charting out the future of the app Photo by Jay Wennington on Unsplash Stepping Back Ok. Let's step back a minute.

  • REACT! -- good boy... NEXT!

    Getting started with React and Next.js -- and some bits about Hue Photo by Robert Larsson on Unsplash A few tidy-ups…

    2 Comments
  • You're my new best friend -- btw, who are you?

    Adding JWT Authentication Photo by Joe Caione on Unsplash Identity Project I've decided not to try to integrate some…

  • It's all about the data, baby.

    Using Entity Framework Core. Photo by Alvan Nee on Unsplash Assumption: Visual Studio and SQL Server are installed I am…

  • Tasks for me and Tasks for you

    An impetus for a new web application -- alleviating my son's confusion. Photo by Ryan Walton on Unsplash I am finally…

  • A Funny Thing Happened on the Way to the API

    So I created a totally blank Python/Django application where there is an app named "backend" with nothing in it -- and…

  • Classic Blunder - not committing my code

    In this article, I'm doing something I should have done at the end of the last one. Dave, you fool! You fell victim to…

  • Brand Spankin’ New Project

    In this article, I'm creating a #GitHub repository, starting a #Python virtual environment, installing #Django, and…

  • On The Road, Again

    Sept. 23, 2024 "Document the Journey" -- David Roberts As an on-again-off-again listener/reader of David Roberts of…

Insights from the community

Others also viewed

Explore topics