C
C#15mo ago
Ice_

❔ Returning the user object that was just created after web api [POST]

I have a web API and the POST works just fine in Swagger. It does work fine if I use the web API from my website application but when I do a PostAsJsonAsync against api/User, I won't get the object back (so I can retrieve the new UserId). I guess this is intended. How would I make the POST method in the UserController to return the object back? A simple "return userobject;" wont suffice!
5 Replies
Ice_
Ice_15mo ago
I fixed it. I had to have a JsonSerializer.Deserialize<User> in the create method of the service on my website. I thought I could just return the object back through a Post
Tvde1
Tvde115mo ago
return Ok(user) or return Created(...) or the best way return StatusCode(StatusCodes.201Created, user) so you get a 201 but don't have to do the mandatory things in the created method this means your endpoint should return a IActionResult. Alternatively your endpoint can just return User
Ice_
Ice_15mo ago
@Tvde1 I still had to use deserialization 🤔 I can simply return a User or use Ok(), Created() etc., it doesn't really matter. The user object did come back (noticed that when I read the httpresponsemessage today), I just had to deserialize it!
Tvde1
Tvde115mo ago
it should deserialize the object for you but if it works, it works ^^
Accord
Accord15mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server
More Posts
❔ How to get file path of PDFs in project.Hello! I am facing an issue with my current project. I want to be able to get a combobox in my Winfo❔ Running ASP.NET file in RiderNot sure why i cant seem to run the file - I'd like to do some testing on this work in progress proj❔ ASP.NET Core Web API controller method parameter is not null, but property values are always null.I have a method on a controller that takes a model class as a parameter. Calling the method from an❔ change timezone of aspnet docker containerhow to change aspnet docker container's timezone? i am using this image `mcr.microsoft.com/dotnet/as❔ Processes and AsyncHow can I recreate this with Async? (BeginOutputReads and such) ```cs // If an error happens, prints❔ Need help translating this psuedocodeHello beginner here for my assignment of translating some psuedocode. i am not sure how to check if❔ Homework Help... Again... Chars and Conditional StatementsHi, for this problem, I am given a char, starter, and an int, number. I have to find the subsequent ❔ I wrote a discord bot but it's not answering me ?``` using DSharpPlus; using DSharpPlus.EventArgs; using System.Threading.Tasks; class Program { ❔ image finally stored as byte but when viewing I'm not getting an imageGood afternoon guy was wondering what I'm doing wrong now for the image not to show? Yes im sorry Homework Help . how to pick out in a string variable that looks like this 1,2 and only pick out the2Greetings everyone i've been having a tought time with some of my homework . I have to pick out in a