.NET Core Web API Call

Introduction 

 
The .net core is an open-platform technology, it will run on Linux, Windows, Mac. The Web API call function reads to a server data value and parses the data objects.
 
Open the command prompt to make a directory on the core console, then create a new .NET console. After restoring the project file, open visual studio code (code.)
  • (win+R) Open Cmd
  • Mkdir coreconsole
  • Cd coreconsole
  • dotnet new console
  • dotnet restore
  • Code .
Declare the head to add the reference to the program file. Get private data HTTP Client request on Base Address URI. After the Default user access, add the MediaTypeWithQualityHeaderValue. The application client, Get a sync (API/USER) ReadAsAsync, needs to add  the System.Net.Http.Headers.Formatting reference. Use the loop condition to refer to the class object value
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Net.Http;  
  4. using System.Net.Http.Headers;  
  5. namespace coreconsole {  
  6.     class Program {  
  7.         static void Main(string[] args) {  
  8.             GetData();  
  9.         }  
  10.         private static void GetData() {  
  11.             HttpClient client = new HttpClient();  
  12.             client.BaseAddress = new Uri("http://localhost:53718/");  
  13.             client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));  
  14.             HttpResponseMessage response = client.GetAsync("api/User").Result;  
  15.             if (response.IsSuccessStatusCode) {  
  16.                 var users = response.Content.ReadAsAsync < IEnumerable < Users >> ().Result;  
  17.                 foreach(var item in users) {  
  18.                     Console.WriteLine(item.Id.ToString() + " " + item.FirstName.ToString() + " " + item.LastName.ToString() + " " + item.Company.ToString() + " " + item.Email.ToString() + " " + item.PhoneNo.ToString());  
  19.                 }  
  20.                 Console.Read();  
  21.             } else {  
  22.                 Console.WriteLine("Error Code" + response.StatusCode + " : Message - " + response.ReasonPhrase);  
  23.                 Console.Read();  
  24.             }  
  25.         }  
  26.     }  
  27.     public class Users {  
  28.         public int Id {  
  29.             get;  
  30.             set;  
  31.         }  
  32.         public string FirstName {  
  33.             get;  
  34.             set;  
  35.         }  
  36.         public string LastName {  
  37.             get;  
  38.             set;  
  39.         }  
  40.         public string Company {  
  41.             get;  
  42.             set;  
  43.         }  
  44.         public string Email {  
  45.             get;  
  46.             set;  
  47.         }  
  48.         public string PhoneNo {  
  49.             get;  
  50.             set;  
  51.         }  
  52.     }  
  53. }  
Open the Terminal (Crtl_Shift+`) to build the project and run the application:
  • Open Terminal (Crtl+Shift+`)
  • dotnet build
  • dotnet run