A standard network interface uses a single-node route for a controller, but multiple methods can be provided through hot reloading. Generally, a controller uses four types of request routes: Create, Read, Update, and Delete (CRUD).1. Get: This method requests data from the server, allowing only data retrieval without writing to the server.2. Post: This method uploads data to the server, requesting the server’s database to add the uploaded data to the system database.3. Put: This method modifies data in the server’s database.4. Delete: This method deletes data from the server’s database.1. Create a new web API project, remove the default controller, and create a new controller, selecting “Controller with read/write actions”. The content of the controller is as follows:Some important feature explanations:This rewritten Get method allows the frontend to pass an int parameter to the server when making a network request, enabling simple parameter interactions, typically used for conditional checks. For example, when the frontend requests with id 1, I return specific data, and for id 2, I return different data, and so on.
[HttpGet("{id}")] // HttpGet with parameter substitution public string Get(int id) { return "value" + id; }
The default server GET route is http://
:80/api/The parameterized Get route is http://
:80/api/For example: http://cjpgxm.club:80/api/test/11 is the id passed to the server (can be 0-9).2. Definition of the Post method:The Post method is used to accept requests for adding content from the frontend. It needs to receive the body of the request (usually in JSON format). When writing the Post method on the server, you need to add [FromBody] before receiving the string, indicating to the system that the content being received is the body of the request, allowing JSON format parameters.
[HttpPost] public void Post([FromBody] string value) { }
The route for the post request: It also only requires using the default route, but the frontend’s request method is set to post, which is not something we need to handle on the backend.Example of a post request:For easier observation, modify the post method to return a response message. In reality, these modifications are not necessary; they are just for debugging convenience. The actual task is to write the received body into the database, which is complex, so we will only do simple debugging here.
[HttpPost] public string Post([FromBody] string value) { return "I have received your request, the data you requested to add is: " + value; }
The default server GET route is http://
3. Similarly, for PUT requests, the server also needs to accept the frontend’s body, so the string parameter must also use the [FromBody] tag. Standard modification requests generally require the frontend to pass an id to modify the corresponding database information based on the id.
[HttpPut("{id}")] public void Put(int id, [FromBody] string value) { // Code to implement the functionality}
The route for the frontend to request PUT to modify data: (the request method is set to put)http://
:80/api/For example: http://cjpgxm.club:80/api/test/1http://cjpgxm.club:80/api/test/2http://cjpgxm.club:80/api/test/3Example:To test and observe, modify the server to respond to the frontend.
[HttpPut("{id}")] public string Put(int id, [FromBody] string value) { return "ok, modified, the new data is: " + value; }
4. Usage of the Delete request:It is relatively simple; the frontend does not need to provide a body, just set the request method to delete. The standard server route generally requires the frontend to provide an id to distinguish which data to delete from the server.The route is the same as the default route; just set the request method to delete, and leave the rest to the server backend to implement.
[HttpDelete("{id}")] public void Delete(int id) { // Code to implement data deletion on the server}
using Microsoft.AspNetCore.Mvc;namespace WebApplication1.Controllers{ [Route("api/[controller]")] // Standard controller does not require multi-node declaration [ApiController] public class TESTController : ControllerBase { // GET: api/test; default response for the route; [HttpGet] public IEnumerable<string> Get() { // Default returns two useless strings; modify functionality as needed return new string[] { "value1", "value2" }; } // GET api/<TEST/id [HttpGet("{id}")] // HttpGet with parameter substitution public string Get(int id) { return "value" + id; } // POST api/<TESTController> [HttpPost] public void Post([FromBody] string value) { } // PUT api/<TESTController>/5 [HttpPut("{id}")] public void Put(int id, [FromBody] string value) { } // DELETE api/<TESTController>/5 [HttpDelete("{id}")] public void Delete(int id) { } }}