Creating a REST API using ASP.NET Core Syntax with Razor Syntax

As I studied articles and content on the web about creating a REST API using the asp.net core, I found out that shaving web pages are not usually used for interfaces. Most of them are focused on Angular 1 && 2 for processing api data coming from the server (Ex: $ http.get, $ http.post). I'm just wondering if there is any article that describes how to use clean razor web pages as an interface for working with web api, or are there any ways to do this correctly using the .net kernel?

For ex:

[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)); } 

And instead of using the Angular $ http service for rendering,

 $http.get("/api/students") .then(function (response) { ... } 

Is there any method for rendering api in razor mode?

0
source share
1 answer

You can use a razor to invoke WebAPI, but this is pretty pointless as you would miss out on all the benefits of modern web development, for example, you would need to send it back to the server and reload the whole page instead of an ajax request and just loading part of the page.

Essentially, you will need to call your webapi from your MVC controller or some facade class inside the controller, get a data response and then print it out with a razor.

I'm not even going to write code for this, since you would be better off using Angular or some JS framework since you will get a better product.

+1
source

Source: https://habr.com/ru/post/1263355/


All Articles