How to Fetch Data in C# .NET Core

We are fetching data from pokeapi to illustrate how we would perform asynchronous operations in c#.

Image for post
Image for post
“black and silver laptop computer on table” by Clément H on Unsplash

But before anything we will setup our environment using visual studio here is a quick guide.

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.

Image for post
Image for post
Create your Solution

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.

Image for post
Image for post

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.

Image for post
Image for post
After Newtonsoft.json nuget package added.

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.

Define your PokeItem model.

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.

here will be aUsing Directive

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.

Define your GetPokemon Method

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.

Get the response, and data from the response.

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.

Log data to your console.

It should display this.

Image for post
Image for post
Result of your data being displayed.

Now we will parse the data to a JObject, and retrieve the results property using bracket notation.

Get Pokemon Method

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.

Define your GetOnePokemon method.

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.

Image for post
Image for post
Log PokeItem name to Console.

Congratulations your just fetched data using c#.

Here is my github repository for reference.

Happy Coding!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store