DEV Community

Cover image for C# (C Sharp) CRUD Rest API using .NET 7, ASP.NET, Entity Framework, Postgres, Docker and Docker Compose

C# (C Sharp) CRUD Rest API using .NET 7, ASP.NET, Entity Framework, Postgres, Docker and Docker Compose

Francesco Ciulla on April 16, 2023

Let's create a CRUD Rest API in C# (or C sharp), using: .NET 7 ASP.NET (Framework for building web apps) Entity Framework (ORM) Postgres (Databa...
Collapse
 
vladissvostok2000 profile image
VladisS-Vostok2000
// POST: api/users
  [HttpPost]
  public async Task<ActionResult<User>> PostUser(User user)
  {
    _context.Users.Add(user);
    await _context.SaveChangesAsync();

    return CreatedAtAction(nameof(GetUser), new { id = user.Id }, user);
  }
Enter fullscreen mode Exit fullscreen mode

Is there expected PostUser?
return CreatedAtAction(nameof(PostUser), new { id = user.Id }, user);

Collapse
 
cawoodm profile image
Marc

Check your code formatting. On iOS Chrome at least it’s garbled.

Collapse
 
francescoxx profile image
Francesco Ciulla

do you mean the brackts?

Collapse
 
cawoodm profile image
Marc

We see:

Let's create two more users, make a POST request to localhost:8080/api/users.

`json
{
"name": "bbb",
"email": "bbb@mail"
}
`
Enter fullscreen mode Exit fullscreen mode
Thread Thread
 
francescoxx profile image
Francesco Ciulla

oh there was a trailing backtick. solved thanks.

Collapse
 
siknik profile image
SikNik

Thanks for the cool tutorial, also watched your video thanks very much!

Not sure if anyone else picked this up from the walkthrough guide on this page above, at the section where we get users? Its pointing to port 8000 which is invalid and the users only return on port 8080, which is correct in accordance with our configs...

The guide here on this page just says otherwise...

📝 Get all users
To get all users, make a GET request to localhost:8000/api/users.

The output should be something like that:

Collapse
 
francescoxx profile image
Francesco Ciulla

oh I will check that, thank you! maybe I was using a different port on that specific part!

Collapse
 
galezovsky profile image
Bartek

You could also add validation for User type to prevent incorrect data and reject request on start, not during writing to database. Obviously that will give you ability to check other things such as e.g. format of e-mail addresses.

Collapse
 
francescoxx profile image
Francesco Ciulla

that would probably be a good idea

Collapse
 
bringablazer profile image
BringABlazer

Very nice - just a question of if it is on purpose that you skipped the explanation of your docker files?

Collapse
 
francescoxx profile image
Francesco Ciulla

fixed, thanks

Collapse
 
vladissvostok2000 profile image
VladisS-Vostok2000

I repeated all steps this awesome guide, but face the problem with creating a user:
MessageText: relation "users" does not exist
I fixed it renaming all colums and table to lower case, and it works.

Collapse
 
francescoxx profile image
Francesco Ciulla

you are correct. I adapted the c# code for a better readability but left the old SQL file. nice catch.

Collapse
 
joetogee profile image
Joe

Super helpful, really helped launch my app. Thank you!

Collapse
 
francescoxx profile image
Francesco Ciulla

you are welcome Joe

Collapse
 
1chris_alex profile image
Christian Alexsander

Can you do a part 2 using entity migrations?

Collapse
 
francescoxx profile image
Francesco Ciulla

that's actually a good idea

Collapse
 
softwaresennin profile image
Lionel♾️☁️

Awesome article @francescoxx I really enjoyed the read.

Please keep it up.

Collapse
 
francescoxx profile image
Francesco Ciulla

you are welcome Lionel!

Collapse
 
nayeemgithub profile image
Nayeem

👍

Collapse
 
francescoxx profile image
Francesco Ciulla

🔥🔥

Collapse
 
kkr1067 profile image
Krishna Kumar

I want to purchase

a Web application complete source code to send WhatsApp Bulk Message.

Preferred technology : Asp.net C# MVC or Core

Please message me if you have .

Whatsapp: 9125474034