.net5编写遵循RESTful设计原则的api接口

using HealthLiteracy.Models.Dto;
using Microsoft.AspNetCore.Mvc;
using System.ComponentModel;

namespace HealthLiteracy.Controllers
{
    /// <summary>
    /// 测试控制器,遵循RESTful接口设计原则
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]    
    public class TTController : ControllerBase
    {
        /// <summary>
        /// 添加方法
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Post([FromBody] AccountDto user)
        {
            return Created($"api/users/{user.Account}", user);
        }

        /// <summary>
        /// 更新方法
        /// </summary>
        /// <param name="id"></param>
        /// <param name="user"></param>
        /// <returns></returns>
        [HttpPut("{id}")]
        public IActionResult Put(int id, [FromBody] AccountDto user)
        {
            return Created($"api/users/{user.Account}", user);
        }

        /// <summary>
        /// 获取所有注释
        /// </summary>
        [HttpGet]
        public void Get()
        {
        }

        /// <summary>
        /// 根据ID获取某一个对象
        /// </summary>
        /// <param name="id"></param>
        [HttpGet("{id}")]
        public void Get(int id)
        {
        }

        /// <summary>
        /// 删除方法
        /// </summary>
        /// <param name="id"></param>
        [HttpDelete("{id}")]
        public void Delete(int id)
        {
        }

        /// <summary>
        /// 删除另一个方法
        /// </summary>
        /// <param name="user"></param>
        [HttpDelete]
        //public void Delete([FromBody] AccountDto user)
        public void DeleteDDD([FromBody] AccountDto user)
        {
        }
    }
}

下面是生成的接口api:

.net5编写遵循RESTful设计原则的api接口

 

上一篇:Docker中部署.Net5项目


下一篇:这难道不是.NET5 的bug? 在线求锤?