【发布时间】:2016-12-17 22:04:28
【问题描述】:
当我在互联网上浏览有关使用 asp.net core 构建 REST API 的文章和内容时,我发现 razor 网页并不常用于前端。他们中的大多数都专注于 Angular 1 && 2 来处理来自服务器的 api 数据(例如:$http.get、$http.post)。我只是想知道是否有任何文章介绍了如何使用纯剃须刀网页作为处理 web api 的前端,或者有什么方法可以使用.net core 正确地做到这一点?
例如:
[Route("api/students")]
public class StudentsController : Controller
{
private IStudentRepository _repository;
public StudentsController(IStudentRepository repository)
{
_repository = repository;
}
[HttpGet("")]
public IActionResult Get()
{
var results = _repository.GetAllStudents();
return Ok(Mapper.Map<IEnumerable<StudentViewModel>>(results));
}
而不是使用 Angular 的 $http 服务在视图中呈现,
$http.get("/api/students")
.then(function (response) {
...
}
有什么方法可以在 Razor 视图中渲染 api 吗?
【问题讨论】:
-
这不是 Razor 的用途。它是在服务器上执行的 HTML 渲染引擎。它不会执行 JavaScript 代码来调用 API。您最终将需要编写代码来做到这一点。你仍然可以使用 Razor 来创建你的 HTML 页面,但我认为如果你用 Razor 以外的其他东西编写该部分会更好。
-
感谢您的评论。这是否意味着我应该使用其他一些语言(例如 jQuery)来将 web api 集成到 razor 网页中? (如果我不打算使用 angular 等)this 方法在 .net 核心中是否仍然相关?
标签: razor asp.net-web-api asp.net-core-mvc