How to Fetch Data in C# .NET Core
We are fetching data from pokeapi to illustrate how we would perform asynchronous operations in c#.
But before anything we will setup our environment using visual studio here is a quick guide.
How to set up your C# and ASP.NET MVC development environment
Learn how to set up C# for web development using the ASP.NET MVC web app framework. Expose your local development…
Great we will now create our solution, which will have a name of PokeApp, and a project which will be a Console app named PokeApi.
Now we will have a Program.cs which will be our main program file. Now we will have another file called PokeItem.cs which would be the structure of each PokeItem in our list.
Now add your Nuget packages needed for your project. Add Newtonsoft which is the most popular nuget package. Used for parsing json to c# objects. By right clicking dependencies, and manage nuget packages.
It should look like this, after adding it.
Now let’s start doing some code.
We will first define our PokeItem.cs file which would be used when we do a api call to just retrieve one pokemon. It is a very basic class with one property, and a constructor method, with the constructor setting it’s name property based on name argument.
Now we will go to our Program.cs file, where we will have a Newtonsoft.Json directive for converting data retrieved data from api to c# objects, and System.Net.Http to use your HttpClient which is the base client for retrieving a response using a uri.
Now we will define a static asynchronous method that will return nothing, but instead logs into a console.
Our using directive will be in a try/catch block.
We will then define our baseUrl within our method which will be the uri that our HttpClient will be connected to.
Then we will have a nested using directive, since it implements the IDisposable interface which has a garbage collector that will collect a object if unused.
We will have three using directives.
The first using directive we will have our HttpClient defined, which will assigned to client. If a exception is caught log it to console.
Now define your response which will be retrieving from the HttpClient response which would be HttpResponseMessage, which would return a message with status code and data from response. For this operation we would use the await keyword for our get request(since it is asynchronous). Which will also nest another using directive which would retrieve the content from the response, by assigning to a variable from the response Content property containing your data.
Now we will just assign a data variable to the content variable in our using directive which will be converted into a string by using the ReadAsStringAsync method, which you would use the await keyword since this operation is asynchronous also. Now just log it to the console if it has data else log the console that it doesn’t have data.
It should display this.
Now we will parse the data to a JObject, and retrieve the results property using bracket notation.
Now we will define your GetOnePokemon method which would be also be static, meaning it would be a class method. Asynchronous since it is performing a call to a api. Then will be void since we are just logging the result into the console. We will define the method similarly to our GetPokemon method, but it take a pokeId argument, which would be use in your url to get a specific pokemon from api.
The only other difference is that we are parsing our data to a JObject, then parsing our JObject name property which is a JToken using string interpolation. To our name parameter of our PokeItem constructor to create a new instance of our PokeItem class. Then we will log our pokeItem name to console.
Our console should look like this.
Congratulations your just fetched data using c#.
Here is my github repository for reference.
Get data from pokeapi using .Net Core. Contribute to AliA1997/Pokemon-App-CSharp development by creating an account on…