.net 核心控制台方法调用
.net core console method call
我正在尝试从 .net 核心控制台应用程序的 Main() 方法调用一个方法,该方法应该显示来自外部 api 的两个属性,但控制台只显示 Hello World 而没有其他结果数据,我还希望控制台保持原样,而不是从屏幕上消失。欢迎大家指点指点,提前致谢!
UserItem.cs -
public class UserItem
{
public UserItem(string name, string url)
{
Name = name;
Url = url;
}
public string Name { get; set; }
public string Url { get; set; }
}
Program.cs -
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
GetUser();
}
// retrieve all.
public static async void GetUser()
{
//baseUrl
string baseUrl = "http://pokeapi.co/api/v2/pokemon/";
try
{
// HttpClient implements a IDisposable interface.
using (HttpClient client = new HttpClient())
{
//initiate Get Request (await will execute the using statement in order).
using (HttpResponseMessage res = await client.GetAsync(baseUrl))
{
//get content from response, then convert it to a c# object.
using (HttpContent content = res.Content)
{
//assign content to data variable by converting into a string using await.
var data = await content.ReadAsStringAsync();
//If the data is not null log convert the data using newtonsoft JObject Parse class method.
if (content != null)
{
//log data object in the console
Console.WriteLine("data------------{0}", JObject.Parse(data)["results"]);
}
else
{
Console.WriteLine("NO Data----------");
}
}
}
}
}
catch (Exception exception)
{
Console.WriteLine("Exception Hit------------");
Console.WriteLine(exception);
}
}
}
using System;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
//Waits the API response
GetUser().Wait();
//Waits until a key is pressed.
Console.ReadKey();
}
// retrieve all.
public static async Task GetUser()
{
//...
}
}
}
我正在尝试从 .net 核心控制台应用程序的 Main() 方法调用一个方法,该方法应该显示来自外部 api 的两个属性,但控制台只显示 Hello World 而没有其他结果数据,我还希望控制台保持原样,而不是从屏幕上消失。欢迎大家指点指点,提前致谢!
UserItem.cs -
public class UserItem
{
public UserItem(string name, string url)
{
Name = name;
Url = url;
}
public string Name { get; set; }
public string Url { get; set; }
}
Program.cs -
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
GetUser();
}
// retrieve all.
public static async void GetUser()
{
//baseUrl
string baseUrl = "http://pokeapi.co/api/v2/pokemon/";
try
{
// HttpClient implements a IDisposable interface.
using (HttpClient client = new HttpClient())
{
//initiate Get Request (await will execute the using statement in order).
using (HttpResponseMessage res = await client.GetAsync(baseUrl))
{
//get content from response, then convert it to a c# object.
using (HttpContent content = res.Content)
{
//assign content to data variable by converting into a string using await.
var data = await content.ReadAsStringAsync();
//If the data is not null log convert the data using newtonsoft JObject Parse class method.
if (content != null)
{
//log data object in the console
Console.WriteLine("data------------{0}", JObject.Parse(data)["results"]);
}
else
{
Console.WriteLine("NO Data----------");
}
}
}
}
}
catch (Exception exception)
{
Console.WriteLine("Exception Hit------------");
Console.WriteLine(exception);
}
}
}
using System;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
//Waits the API response
GetUser().Wait();
//Waits until a key is pressed.
Console.ReadKey();
}
// retrieve all.
public static async Task GetUser()
{
//...
}
}
}