以下是一个示例代码,展示了如何在ASP.NET Core Razor页面中调用REST服务并等待响应:
@page
@model IndexModel
@{
ViewData["Title"] = "Home page";
}
@ViewData["Title"]
@if (Model.Response != null)
{
Response: @Model.Response
}
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.Net.Http;
using System.Threading.Tasks;
public class IndexModel : PageModel
{
public string Response { get; private set; }
public async Task OnPostAsync()
{
using (var client = new HttpClient())
{
// 发送GET请求到REST服务的URL
var response = await client.GetAsync("https://api.example.com/rest/service");
// 确保响应成功
response.EnsureSuccessStatusCode();
// 读取响应内容
Response = await response.Content.ReadAsStringAsync();
}
}
}
请注意,以上代码示例中的REST服务URL仅作为示例,您需要根据实际情况将其替换为您要调用的REST服务的URL。