在ASP.NET Core开发中,前端和后端的连接可以通过API控制器实现。
例如,定义一个GET请求接口来获取后端数据
[ApiController]
[Route("[controller]")]
public class DataController : ControllerBase
{
[HttpGet]
public IEnumerable Get()
{
return new string[] { "data1", "data2", "data3" };
}
}
在前端,您可以使用HTTP请求来获取数据
fetch('/Data')
.then(response => response.json())
.then(data => console.log(data))
从后端获取数据后,您可以在前端使用它以任何适当的方式呈现。