C# 如何在WPF.NETCore3.1中将API连接到我的项目

C# 如何在WPF.NETCore3.1中将API连接到我的项目,c#,wpf,api,.net-core-3.1,C#,Wpf,Api,.net Core 3.1,我有API和链接 下面是我的模型课 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace VPN.Model { public class ServerModel { public string error { get; set; } public string message {

我有API和链接

下面是我的模型课

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace VPN.Model
{
public class ServerModel
{
    public string error { get; set; }
    public string message { get; set; }
    public Server[] servers { get; set; }
}

public class Server
{
    public string sid { get; set; }
    public string country { get; set; }
    public string dns { get; set; }
    public string port { get; set; }
    public string psk { get; set; }
    public string pptp { get; set; }
    public string l2tp { get; set; }
    public string tcp { get; set; }
    public string udp { get; set; }
    public string openconnect { get; set; }
    public string ikev2 { get; set; }
    public string sstp { get; set; }
    public string p2p { get; set; }
    public string videostreaming { get; set; }
    public string security { get; set; }
    public string voip { get; set; }
    public string enable { get; set; }
    public string maintmode { get; set; }
    public string iso { get; set; }
    public string free { get; set; }
    public string recent { get; set; }
    public string time { get; set; }
    public string fav { get; set; }
    public int Pingrate { get; set; }
    public string IsFavorite { get; set; }

    public string FavProtocol { get; set; }
}
}

我想将我的API连接到.Net项目,并将API中的数据存储在一个列表中,以便以后可以在项目中使用它,例如listview来选择任何服务器来连接VPN,并显示国家名称、城市、pingrate等。

实现这一点的方法有很多种,这种方法不需要额外的依赖项

obj将结果反序列化到ServerModel类中

 HttpClient client = new HttpClient();
 client.BaseAddress = new Uri("https://webservice.casvpn.com/");
 HttpResponseMessage response = client.GetAsync("serverlist.php").Result; 
 if (response.IsSuccessStatusCode)
 {
      string result = response.Content.ReadAsStringAsync().Result; 
      var obj = System.Text.Json.JsonSerializer.Deserialize<ServerModel>(result);   
 }

到目前为止你试过什么。。。?您可以使用WebClient或RestSharp(第三方nuget软件包)调用web服务,并在您的对象中对其进行反序列化。我对api是新手,我了解这个概念,但我无法实现api,因此不清楚如何使用api。我添加了一个对我有用的答案,但这是我的首选方法,其他人可能会建议其他方法。如今,当大多数人谈论
API
s时,他们只是指进行HTTP调用,通常使用JSON有效负载。您可以使用HttpClient类轻松地发出HTTP GET和POST请求。要创建或解析JSON,可以使用.NETCore的JsonSerializer类或JSON。NET@PanagiotisKanavos我有一个listview,我只是想在项目中将数据从api填充到我的listview,其中有dns,可以用作连接到vpn的链接。在这种情况下,这些包没有任何好处-同样可以使用内置的HttpClient编写和System.Text.Json,尽可能多的行。它也会跑得更快。如果你想建议使用不同的软件包,你必须解释它们提供了什么,我对此表示赞赏,这就是为什么我在回答中说这是我首选的方法。首选为什么?为什么要添加额外的依赖项和代码?你从中得到了什么?否则,答案就没有多大帮助,尤其是对于那些不知道选择或权衡的人,我同意他们的观点,他们完全支持减少依赖性,并相应地更新了我的答案。我的大部分工作都是在有多个RESTClient等的大型项目上完成的,因此我个人喜欢RestSharp。谢谢Ryan,而且我有一个window.xaml,其中有一个listview,我想在该窗口中显示所有这40台服务器。我怎样才能做到这一点。?
lvwServer.ItemsSource = obj.servers;
lvwServer.DisplayMemberPath = "dns"; //Or another property of Server you want to display