如何在ASP.NET Razor Web应用程序中调用ASP.NET API
我正在构建一个带有Mongo数据库的ASP.NET Core 6 Web应用程序和一个带有Mongo数据库的ASP.NET API。我有API,它返回JSON中的正确数据,但是我不知道如何在Web应用程序中调用此数据并在剃须刀页面上显示数据。这是我到目前为止的代码
无法分配请求的地址(Localhost:52189)
这是我在控制器类中的代码
public class IncidentController : Controller
{
private static readonly String conn = "https://localhost:52189/api/incident/";
public static async Task<List<IncidentModel>> Index()
{
List<IncidentModel> incidents = new List<IncidentModel>();
using (var httpClient = new HttpClient())
{
using (var response = await httpClient.GetAsync(conn))
{
string apiResponse = await response.Content.ReadAsStringAsync();
incidents = JsonConvert.DeserializeObject<List<IncidentModel>>(apiResponse);
}
}
return incidents;
}
}
,然后是剃须刀页面(index.cshtml.cs)
public class IndexModel : PageModel
{
private readonly ILogger<IndexModel> _logger;
public dynamic incidentList;
public IndexModel(ILogger<IndexModel> logger)
{
_logger = logger;
GetIncidents();
}
public async void GetIncidents()
{
this.incidentList = await IncidentController.Index();
}
}
,我如何正确调用此API并在index上显示数据.cshtml
页面?任何帮助或教程链接将不胜感激。
I'm building an ASP.NET Core 6 web app with Razor Pages and an ASP.NET API with a Mongo database. I have the API and it returns the correct data in Json, however I can't figure out how to call this in the web app and display the data on a Razor page. This is the code i have so far but every time i run this I get this exception
Cannot assign requested address (localhost:52189)
This is the code I have in the controller class
public class IncidentController : Controller
{
private static readonly String conn = "https://localhost:52189/api/incident/";
public static async Task<List<IncidentModel>> Index()
{
List<IncidentModel> incidents = new List<IncidentModel>();
using (var httpClient = new HttpClient())
{
using (var response = await httpClient.GetAsync(conn))
{
string apiResponse = await response.Content.ReadAsStringAsync();
incidents = JsonConvert.DeserializeObject<List<IncidentModel>>(apiResponse);
}
}
return incidents;
}
}
And then the Razor page (index.cshtml.cs)
public class IndexModel : PageModel
{
private readonly ILogger<IndexModel> _logger;
public dynamic incidentList;
public IndexModel(ILogger<IndexModel> logger)
{
_logger = logger;
GetIncidents();
}
public async void GetIncidents()
{
this.incidentList = await IncidentController.Index();
}
}
Basically, how can I correctly call this API and display the data on the index.cshtml
page? Any help or tutorials links will be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
,更改下面的代码,然后您可以在ASP.NET剃须刀中调用ASP.NET API并获取JSON:
此外:下面是工作演示(显示)关于在剃须刀页面上显示数据的index.cshtml中的汽车列表,您可以参考它:
CAR
INDEXMODEL
INDEX.CSHTML:
结果:
In your IndexModel, change code like below, then you can call an ASP.NET API in an ASP.NET Razor and get the json :
Besides below is work demo(show the list of cars in the Index.cshtml) about display the data on a Razor page, you can refer to it:
Car
IndexModel
Index.cshtml:
Result:
您可以使用httpclient调用API终点,这是代码。
您也可以添加标题。请验证您的API终点链接
You can call an API end point using httpClient Here is the code.
You can add headers as well. Please verify your api end point link