Asp.Net Core: Create Api interface
To create an Api interface in Asp.Net Core projects, create a subfolder / api in the controller directory
Add them to the context-> Add new Item-> Add Scaffold
Enter an API Controller Empty
After the MVC nomenclature one names this as the controller for web pages themselves
Here ProjectsController
Api Controller -> create method
In the controller, you just have to describe a standard method, such as Index (default) and assign the list in return as the output.
This example creates a list of projects
from the data model type Project
// GET: /api/index public List<ProjectModel> Index() { ///-------------< Index >-------------
//--< Get Linq.Query >-- //*gets last 10 Projects with View_Sum var query = (from n in _dbContext.tbl_Projects where n.IsDraft == false orderby n.IDProject descending select n ).Take(50); //--</ Get Linq.Query >--
//----< fill Data_to_View >---- List<ProjectModel> dataList = query.ToList<ProjectModel>();
//< out > //*output to client return dataList; //</ out > ///-------------</ Index >------------- } |
Test output in the browser
In the browser you can simply test the default address for MVC Projecte enter
https://localhost:44390/api/projects
then the results are output as a json list
Example HTML texts Output via json api list
{"idProject":411,"idOwner":1,"title":"🎬 Fliese mit der Flex schneiden","text":" man kann Fliesen ganz gut mit der Flex schneiden hierzu muss man aber 3 dinge beachten mit einem Holz kann man die Scheibe stabilisieren die Fliese von oben schneiden die Trennscheibe in die richtige Richtung drehen lassen der Schnitt ist meistens leicht schräg die untere Seite ergibt einen sauberen Schnitt die vordere Seite franst leicht aus Deshalb die rückseitige verwenden ","html":" \r\n <div>man kann Fliesen ganz gut mit der Flex schneiden. hierzu muss man aber 3 dinge beachten:</div><div><a href=\"" rel=\"nofollow\" style=\"background-color: rgb(255, 255, 255);\"><iframe width=\"560\" height=\"315\" src=\"https://www.youtube.com/embed/R-pkGs_y9Og\" frameborder=\"0\" allow=\"autoplay; encrypted-media\" allowfullscreen=\"\"></iframe></a><br></div><div>mit einem Holz kann man die Scheibe stabilisieren</div><div>die Fliese von oben schneiden</div><div>die Trennscheibe in die richtige Richtung drehen lassen</div><div>der Schnitt ist meistens leicht schräg: die untere Seite ergibt einen sauberen Schnitt, die vordere Seite franst leicht aus</div><div><br></div><div>Deshalb die rückseitige verwenden.</div><img src=\"/User_Files/Notes/Images/Image_411_0_blog.jpg\" style=\"width: 100%; max-width: 100%; height: auto; display: inline-block;\"><br><br><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div> \r\n \r\n \r\n ","urlRef":null,"folder":"Bauen;Fliesen;Tipp","dtEdit":"2018-06-10T12:03:07.047","dtCreated":"2018-06-10T11:58:20.15","isDraft":false},{"idProject":410,"idOwner":1,"title":"🎬 Kartoffel als Setzling Unterlage für Pfirsiche","text":" ","html":" \r\n
|
Output in the Chrome browser
Complete code for the Api controller
File, Code: Controllers / api / ProjectsController.cs
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Freelance.Data; using Freelance.Models; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc;
namespace Freelance.Controllers.api {
[Produces("application/json")] [Route("api/Projects")] public class ProjectsController : Controller { //--------------< Class: Controller >--------------------- #region Controller Init private readonly ApplicationDbContext _dbContext;
public ProjectsController(ApplicationDbContext dbContext) { //----< Init: Controller >---- _dbContext = dbContext; //----</ Init: Controller >---- } #endregion
// GET: /api/index public List<ProjectModel> Index() { ///-------------< Index >-------------
//--< Get Linq.Query >-- //*gets last 10 Projects with View_Sum var query = (from n in _dbContext.tbl_Projects where n.IsDraft == false orderby n.IDProject descending select n ).Take(50); //--</ Get Linq.Query >--
//----< fill Data_to_View >---- List<ProjectModel> dataList = query.ToList<ProjectModel>();
//< out > //*output to client return dataList; //</ out > ///-------------</ Index >------------- }
//--------------</ Class: Controller >--------------------- } }
|