demand

By creating a “to-do” task demo, you can learn and master ASP.NET Core.

The to-do list has the following functions:

  • Get all to-do items
  • Gets the item by ID
  • Add a new item
  • Update an existing item
  • Delete the item

Creating a Web project

  • From the File menu, choose New > Project.
  • Select the ASP.NET Core Web Application template, and then click Next.
  • Name the project Course001, and click Create.
  • In the Create New ASP.NET Core Web Application dialog box, be sure to select.NET Core and ASP.NET Core 3.1. Select the API template, and then click Create.

Adding a Controller

  • Right-click the Controllers folder.

  • Choose Add > New Build item.

  • Select “API Controller with Read/write operations” and then select “Add”.

  • Named “TodoController. Cs”.

  • Select Add.

    using Microsoft.AspNetCore.Mvc; using System.Collections.Generic; namespace Course001.Controllers { [Route(“api/[controller]”)] [ApiController] public class TodosController : ControllerBase { [HttpGet] public IEnumerable Get() { return new string[] { “value1”, “value2” }; } [HttpGet(“{id}”)] public string Get(int id) { return “value”; } [HttpPost] public void Post([FromBody] string value) { } [HttpPut(“{id}”)] public void Put(int id, [FromBody] string value) { } [HttpDelete(“{id}”)] public void Delete(int id) { } } }

Pass the Postman test Get

  • Create a new request.
  • Set the HTTP method to “GET”.
  • Sets the request URI to https://localhost:44342/api/todos.
  • Choose to Send.

summary

At this point, the WebApi project is complete and Postman allows you to perform various tests on the interface.

reference

  • Docs.microsoft.com/zh-cn/aspne…