【问题标题】:WEB API post from uri/ Query string in post来自 uri/ 帖子中的查询字符串的 WEB API 帖子
【发布时间】:2020-10-13 01:06:50
【问题描述】:

我有模特

    public partial class TalentVendorShots
    {
        public int Id { get; set; }
        public string Email { get; set; }
        public string One { get; set; }
        public string Two { get; set; }
        public string Three { get; set; }
        public string Four { get; set; }
        public string Five { get; set; }
        public string Six { get; set; }
        public string Seven { get; set; }
        public string Eight { get; set; }
        public string Nine { get; set; }
        public string Ten { get; set; }
    }

和基本控制器

    [Route("api/[controller]")]
    [ApiController]
    public class TalentVendorShotsController : ControllerBase
    {
        private readonly champagneDatabase _context;

        public TalentVendorShotsController(champagneDatabase context)
        {
            _context = context;
        }

        // GET: api/TalentVendorShots
        [HttpGet]
        public async Task<ActionResult<IEnumerable<TalentVendorShots>>> GetTalentVendorShots()
        {
            return await _context.TalentVendorShots.ToListAsync();
        }

        // GET: api/TalentVendorShots/5
        [HttpGet("{id}")]
        public async Task<ActionResult<TalentVendorShots>> GetTalentVendorShots(int id)
        {
            var talentVendorShots = await _context.TalentVendorShots.FindAsync(id);

            if (talentVendorShots == null)
            {
                return NotFound();
            }

            return talentVendorShots;
        }

        // PUT: api/TalentVendorShots/5
        [HttpPut("{id}")]
        public async Task<IActionResult> PutTalentVendorShots(int id, TalentVendorShots talentVendorShots)
        {
            if (id != talentVendorShots.Id)
            {
                return BadRequest();
            }

            _context.Entry(talentVendorShots).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!TalentVendorShotsExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return NoContent();
        }

        // POST: api/TalentVendorShots
        [HttpPost]
        public async Task<ActionResult<TalentVendorShots>> PostTalentVendorShots(TalentVendorShots talentVendorShots)
        {
            _context.TalentVendorShots.Add(talentVendorShots);
            await _context.SaveChangesAsync();

            return CreatedAtAction("GetTalentVendorShots", new { id = talentVendorShots.Id }, talentVendorShots);
        }

        // DELETE: api/TalentVendorShots/5
        [HttpDelete("{id}")]
        public async Task<ActionResult<TalentVendorShots>> DeleteTalentVendorShots(int id)
        {
            var talentVendorShots = await _context.TalentVendorShots.FindAsync(id);
            if (talentVendorShots == null)
            {
                return NotFound();
            }

            _context.TalentVendorShots.Remove(talentVendorShots);
            await _context.SaveChangesAsync();

            return talentVendorShots;
        }

        private bool TalentVendorShotsExists(int id)
        {
            return _context.TalentVendorShots.Any(e => e.Id == id);
        }
    }
}

所有这些都可以正常工作。我从数据库中获取信息很好。现在我想通过 uri 在桌子上发帖。没有身体。例如 /api/TalentVendorShots/id=1,email=testemail 应该创建一个 id 为 1 和电子邮件为 testemail 的新记录。我怎样才能做到这一点?

【问题讨论】:

    标签: c# asp.net entity-framework asp.net-web-api


    【解决方案1】:

    基本规则是,如果操作不是幂等,则应使用 POST。虽然您可以将查询参数传递给 POST,但没有正文。但这在这种情况下没有意义。基本上查询参数用于获取/过滤信息。

    与 ARC、Swagger 和 PostMan(chrome 扩展不允许,但独立应用程序允许)等许多 Web API 测试工具类似,不允许使用 GET 请求发送正文。虽然您可以在 GET 请求中发送正文。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-16
      • 2014-03-17
      • 1970-01-01
      • 2020-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-29
      相关资源
      最近更新 更多