❔ Call an API, get information and then display it on an ASP.NET app
hello,
I have created a console application that can retrieve data from an API.
Now I want to display this data on an ASP.NET webapp, but I can't find anything on how to do this. Can anyone help me get started?
55 Replies
Fetch the data in your controller action, send it to the view?
Yes, but I dont know how to do that and I am not sure where to get started
I cant find anything on the web
Have you ever made an ASP webapp before?
Not really
okay, so create one and use the template you want
I've gone ahead and looked at the basics of making the asp webapp using visual studio code
The thing I am struggling with is, is where to put the async task of retrieving the results and then how to display the results
You'll likely want either the webapp razor template, or webapp MVC
literally anywhere
i have mvc I think
you can make a service class and register that with your DI, or add it directly in the controller
to display the values, you call that method in your controller action and pass the data into your view
I tried to add it directly in the controller but that didnt work at all
show me
One sec
Okay so I have this Controller class
It doesnt like me creating a HTTPClient and then it also doesnt recognize the variable
uhm
that code is outside the class
you can't just put code anywhere you want mate
yea I fixed that now
xD
Now I have this sorry about that
There is so much wrong here
I guess
never use
using
on a HttpClienteh I used it in my console app
The microsoft documentation used it like that
no they don't
they actively say you should not
HttpClient is intended to be instantiated once and reused throughout the life of an application.also, you can't just slap
async
on a method
you need to change the returntype to async Task<string>
and GetTopProduct
also needs to return Task<string>
okay okay I fixed that
It works omg haha
So I should define my HTTPClient in the Index() function?
Or is that a bad idea still
the best answer is to use IHttpClientFactory and inject the client
but an easier solution is to use a static httpclient
So this is better then?
yes
even better would be not messing with the client defaults and instead setting that in a request message
Ah okay
Thank you so much, sorry about my cursed code I started with c# two days ago for a job interview xD
What other languages do you know and for how long have you been coding?
Like a couple years now on and off, I know java python and javascript
Okay. Java is very similar to C# in how it works under the hood
so the whole class/method stuff etc
Yea the syntax is also kinda similar which is nice
throw everything from python and JS away 😄
Haha yea thats a lot more simple
But this company uses .NET only so I will have to get used to c#
its a very good language when you get used to it
Yea its used a lot I heard
Can I print an HTML table at this return line too?
uhm
use a view
if this is MVC, you'd
return View(yourViewModel);
where yourViewModel is just a POCO that contains all the data you want to display
And then I change this so that it displays the table?
something like this
ahhhh okay I think I get it
and then I create the model in the models folder right? Because for some reason it cant find my model
you can create the model anywhere
you just need to fully qualify the type in your
@model
line
as above.Ooh I need to specify the object there I got it
I put the whole file xD
Hmm, I wrote this now
But I cant pass s.Values in the View, should I convert it to some different object?
yes
a model you control
Well GTINKeeper is a model right?
sure but
s.Values
isn't
I defined it like this
Isn't s.Values a list of all the GTINKeepers?
what is the type definition of
s.Values
?Ah it's a collection
ofc it is
which I cant pass in there I guess
Can I pass a list of all the values?
you can, but perhaps turn it into a nicer type first
like an array, or a List
yea I created a list now
but that still cant be passed in the View() function I guess im missing something here
did you make your
@model
a list?No I did not its a single object
but now I did
Still doesnt work though
Got these 2 now
use
ToList
instead of making your own list
but yeah, that should work?
This?
Because that doesnt do it either
ah your return type on the method is wrong
it should just be
Task<IActionResult>
Ah let me try that
That worked! I can create a table with that thanks
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.