✅ Some terrible code
Soooooooooooooooo, I have no clue how to make HTTP requests. Can anyone advise a nice guide? Like, that's what i have built with ChatGPT's help. I just need some advice, except to stop coding 🙂
38 Replies
This looks good enough, what's the problem?
Unless you just want to make it better in general?
Yep, something like this. Maybe anyone has some good guides about this topic cuz core 7 is kinda new as i get, and finding something useful is a bit harder then other things
Well, one obvious improvement would be using dependency injection to get the database context instead of
new
ing it up
Far as guides go, Microsoft's docs are good enough for beginnerslike this one?
Tutorial: Call an ASP.NET Core web API with JavaScript
Learn how to call an ASP.NET Core web API with JavaScript.
For example, yes
And this one, since you're using Minimal APIs on the backend: https://learn.microsoft.com/en-us/aspnet/core/tutorials/min-web-api?view=aspnetcore-7.0&tabs=visual-studio
Tutorial: Create a minimal API with ASP.NET Core
Learn how to build a minimal API with ASP.NET Core.
So, what do you mean? Are you suggesting that I could create custom middleware which, upon creation, initializes an instance of the database using a class constructor?
No
I mean what I said
Use dependency injection
Register the context in the services
And inject it via a parameter in your lambda
like this?
Almost
EF has its own method of registering in the services
Then you inject it like you have it here, yes
But with
MyCoolDbContext
not any interfaceOh, now it even works. Thank you very much!
also
why
Persosn
Also, why do you read the data from the httpcontext?
You can just bind it
is that a typo?
No need to use
request
and response
directly, it's not Node.js lolfixed lmao
its only way i found how to do that
There
im so dumb lmao
rly can't get
whats a problem here
Well, you don't run any async code here
So that await is kinda useless
You should change
FirstOrDefault()
into FirstOrDefaultAsync()
and await
thatyep, thx :), but this thing is still here
Where does this error happen?
That's... very weird
I guess you could try explicity addng a return type to this lambda...?
more errors lmao
nmv about that
Thank you very much again
I'll come up with something
Task<IResult>
maybe
Not as type-safe but still
Regardless, even the initial error, about RequestDelegate
not taking 2 arguments, should not be a thingnow it works
Nice
had do add last return but i don't think its important at all rn 🙂
this way betterr
Oh yeah
Thanks again! You've saved me over 30 hours of googling time 🙂
Anytime