Return 来自网络的列表 API C#
Return List from a web API in C#
tl;dr:我有一个可以在浏览器中运行的 API,但我不知道如何 return 项目列表以便我可以在列表中遍历它们。
长版:
我仍在与 API 作斗争,现在我遇到了另一个障碍。我的 API:
中有以下代码
[Route("api/[controller]")]
[ApiController]
public class ScheduleController : Controller
{
[HttpGet]
public IEnumerable<Schedule> GetAllSchedules()
{
using (ScheduleDataEntities entities = new ScheduleDataEntities())
{
return entities.Schedule.ToList();
}
}
[HttpGet("{id}")]
public Schedule GetIndividualSchedule(int id)
{
using (ScheduleDataEntities entities = new ScheduleDataEntities())
return entities.Schedule.FirstOrDefault(e => e.ScheduleID == id);
}
}
当我导航到我的本地主机 (https://localhost:7017/api/schedule) 时,它 return 数据很好:
[{"userID":121,"weekDay":"Monday","startTime":"07:00:00","endTime":"07:15:00","createdDate":"2022-01-18T22:11:34.8966667","modifiedDate":"2022-01-18T22:11:34.8966667","active":false,"scheduleID":14414,"dayOfWeek":1,"tsType":"Normal"},
{"userID":94,"weekDay":"Wednesday","startTime":"08:45:00","endTime":"09:00:00","createdDate":"2021-11-03T13:50:50.26","modifiedDate":"2021-11-03T13:50:50.26","active":false,"scheduleID":13160,"dayOfWeek":3,"tsType":"Normal"}
...]
到目前为止,还不错。现在,我想将 return 列表发送给调用 API 的客户端。我做了一个简单的测试客户端,当我按下一个按钮时应该得到列表:
client.Dispose();
client = new HttpClient();
client.BaseAddress = new Uri("https://localhost:7017/api/schedule/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
List<Schedule> list = GetScheduleList("https://localhost:7017/api/schedule");
//Do something with the list...
但是,我不知道这个方法应该如何工作:
static List<Schedule> GetScheduleList(string path)
{
List<Schedule> schedules = null;
HttpResponseMessage response = client.GetAsync(path);
return schedules;
}
我在网上找到的每个教程都说要使用 GetAsync。但是,这仅适用于 await 关键字。添加 await 关键字意味着我必须将 async 关键字添加到我的方法中,这意味着我必须 return 一个 Task 或类似任务的类型,它们不能转换为 List。如果我 return 将其作为一项任务,我该如何将其分解回我的日程表列表?
我真的觉得我已经尝试过我遇到的任何事情,所以非常感谢你能提供的任何帮助。
试试这个
static async Task<List<Schedule>> GetScheduleList(string path)
{
var response = await client.GetAsync(path);
if (response.IsSuccessStatusCode)
{
var stringData = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<List<Schedule>>(stringData);
}
return null;
并修复方法调用
List<Schedule> list = await GetScheduleList("https://localhost:7017/api/schedule");
或者你可以试试,如果你不使用 async
List<Schedule> list = GetScheduleList("https://localhost:7017/api/schedule").Result;
您是否尝试在调试模式下启动 Web 应用程序并找到 API 的 return?
您甚至可以使用 console.log 来提供更多详细信息;
要不然哪天我也去了。这是CORS问题。您应该添加 header :Access-Control-Allow-Origin 应该具有通配符“*”作为值,以便能够在 http 服务器上调用 https APIs。
tl;dr:我有一个可以在浏览器中运行的 API,但我不知道如何 return 项目列表以便我可以在列表中遍历它们。
长版: 我仍在与 API 作斗争,现在我遇到了另一个障碍。我的 API:
中有以下代码[Route("api/[controller]")]
[ApiController]
public class ScheduleController : Controller
{
[HttpGet]
public IEnumerable<Schedule> GetAllSchedules()
{
using (ScheduleDataEntities entities = new ScheduleDataEntities())
{
return entities.Schedule.ToList();
}
}
[HttpGet("{id}")]
public Schedule GetIndividualSchedule(int id)
{
using (ScheduleDataEntities entities = new ScheduleDataEntities())
return entities.Schedule.FirstOrDefault(e => e.ScheduleID == id);
}
}
当我导航到我的本地主机 (https://localhost:7017/api/schedule) 时,它 return 数据很好:
[{"userID":121,"weekDay":"Monday","startTime":"07:00:00","endTime":"07:15:00","createdDate":"2022-01-18T22:11:34.8966667","modifiedDate":"2022-01-18T22:11:34.8966667","active":false,"scheduleID":14414,"dayOfWeek":1,"tsType":"Normal"},
{"userID":94,"weekDay":"Wednesday","startTime":"08:45:00","endTime":"09:00:00","createdDate":"2021-11-03T13:50:50.26","modifiedDate":"2021-11-03T13:50:50.26","active":false,"scheduleID":13160,"dayOfWeek":3,"tsType":"Normal"}
...]
到目前为止,还不错。现在,我想将 return 列表发送给调用 API 的客户端。我做了一个简单的测试客户端,当我按下一个按钮时应该得到列表:
client.Dispose();
client = new HttpClient();
client.BaseAddress = new Uri("https://localhost:7017/api/schedule/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
List<Schedule> list = GetScheduleList("https://localhost:7017/api/schedule");
//Do something with the list...
但是,我不知道这个方法应该如何工作:
static List<Schedule> GetScheduleList(string path)
{
List<Schedule> schedules = null;
HttpResponseMessage response = client.GetAsync(path);
return schedules;
}
我在网上找到的每个教程都说要使用 GetAsync。但是,这仅适用于 await 关键字。添加 await 关键字意味着我必须将 async 关键字添加到我的方法中,这意味着我必须 return 一个 Task 或类似任务的类型,它们不能转换为 List。如果我 return 将其作为一项任务,我该如何将其分解回我的日程表列表?
我真的觉得我已经尝试过我遇到的任何事情,所以非常感谢你能提供的任何帮助。
试试这个
static async Task<List<Schedule>> GetScheduleList(string path)
{
var response = await client.GetAsync(path);
if (response.IsSuccessStatusCode)
{
var stringData = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<List<Schedule>>(stringData);
}
return null;
并修复方法调用
List<Schedule> list = await GetScheduleList("https://localhost:7017/api/schedule");
或者你可以试试,如果你不使用 async
List<Schedule> list = GetScheduleList("https://localhost:7017/api/schedule").Result;
您是否尝试在调试模式下启动 Web 应用程序并找到 API 的 return? 您甚至可以使用 console.log 来提供更多详细信息;
要不然哪天我也去了。这是CORS问题。您应该添加 header :Access-Control-Allow-Origin 应该具有通配符“*”作为值,以便能够在 http 服务器上调用 https APIs。