gpt4 book ai didi

c# - ASP.NET - 模型对象列表的 API Url

转载 作者:行者123 更新时间:2023-11-30 13:25:20 25 4
gpt4 key购买 nike

目前我正在通过 jQuery 进行 API 调用,我的问题是,是否可以在 C# 中进行此调用或将 API 调用的结果转换为 ASP.NET 模型对象列表?

这是我的模型

public class TeamStatsClass
{
public int id { get; set; }
public string name { get; set; }
public string league { get; set; }
public string division { get; set; }
}

这是我当前的 ajax 调用

$.ajax({
url: "https://statsapi.web.nhl.com/api/v1/teams?sportId=1",
success: function (data) {

for (var team of data.teams) {

console.log(team.name);

}

}
});

更新

我把我的类(class)改成了这样:

public class StatsTeamsClass
{
public IEnumerable<Teams> teams { get; set; }
public string copyright { get; set; }
}
public class Division
{
public int id { get; set; }
public string name { get; set; }
public string link { get; set; }
}

public class Teams
{
public int id { get; set; }
public string name { get; set; }
public string link { get; set; }
public League league { get; set; }
public Division division { get; set; }
}

并创建了这个确实将结果放入模型对象的方法:

public async System.Threading.Tasks.Task<StatsTeamsClass> GetTeams()
{

HttpClient Http = new HttpClient();
var json = await Http.GetStringAsync("https://statsapi.web.nhl.com/api/v1/teams?sportId=1");
StatsTeamsClass teams = JsonConvert.DeserializeObject<StatsTeamsClass>(json);

return teams;
}

但是当我尝试在另一个 Controller 中调用这个方法时,它只是卡在那里,没有错误,什么也没有,我假设它会在一段时间后超时

public class HomeController : Controller
{

APIController webService = new APIController();

public ActionResult Index()
{

var item = webService.GetTeams().Result.teams;

return View();
}
}

(GetTeams() 在 Controller APIController 内部)

那么 A. 获取对象模型中 API 的结果然后调用这些结果的正确方法是什么?

最佳答案

Controller 操作也需要异步,以避免混合异步等待和阻塞调用,如 .Result.Wait() 可能会导致死锁。

引用 Async/Await - Best Practices in Asynchronous Programming

public class HomeController : Controller {    
APIController webService = new APIController();

public async Task<ActionResult> Index() {
var model = await webService.GetTeams();
var teams = model.teams;
return View();
}
}

假设 APIController 是一个实际的 ApiContoller

public class APIController : ApiController {

//Your original code
public async Task<StatsTeamsClass> GetTeams() {
HttpClient Http = new HttpClient();
var json = await Http.GetStringAsync("https://statsapi.web.nhl.com/api/v1/teams?sportId=1");
StatsTeamsClass teams = JsonConvert.DeserializeObject<StatsTeamsClass>(json);
return teams;
}

//...
}

我建议不要像从 HomeController 那样直接调用 APIController,而是将 GetTeams() 方法提取到可重用服务中

public class WebService {
static Lazy<HttpClient> http = new Lazy<HttpClient>();

public async Task<T> GetAsync<T>(string url) {
var json = await http.Value.GetStringAsync(url);
return JsonConvert.DeserializeObject<T>(json);
}

public Task<StatsTeamsClass> GetTeamsAsync() {
var url = "https://statsapi.web.nhl.com/api/v1/teams?sportId=1";
return GetAsync<StatsTeamsClass>(url);
}
}

引用 You're using HttpClient wrong

可以在 HomeController 中正确使用

public class HomeController : Controller {        
public async Task<ActionResult> Index() {
// Ideally web service should be injected but that topic
// is outside of the scope of the question at the moment.
var webService = new WebService();

var model = await webService.GetTeamsAsync();
var teams = model.teams;

//...

return View(teams);
}
}

这里假设项目是混合的 Asp.Net MVC 和 Web Api 2+

Index.cshtml

@model IEnumerable<Teams>
@{
ViewBag.Title = "Teams";
}
@if(Model != null && Model.Count() > 0) {
@foreach (var @team in Model) {
<p>@team.name</p>
}
}

关于c# - ASP.NET - 模型对象列表的 API Url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57681576/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com