【问题标题】:ASP.NET Core - Trying to create an endpoint which deletes multiple records by IDASP.NET Core - 尝试创建一个按 ID 删除多条记录的端点
【发布时间】:2022-01-04 21:18:51
【问题描述】:

我正在尝试创建一个应该删除多个 ID 的端点。它应该匹配deleteRoom(ids: number[])。您可以在下面看到我尝试过的内容,但它与 angular 的请求不匹配。

deleteRoom(ids: number[]) {
  return this.httpClient.delete(`${this.actionUrl}?id=${ids.toString()}`);
}
public class RoomsController : ApiControllerBase
{
    [HttpGet]
    public async Task<ActionResult<IList<RoomDto>>> GetRooms()
    {
        var result = await Mediator.Send(new GetRoomsQuery()).ConfigureAwait(false);

        return Ok(result);
    }

    [HttpGet("available")]
    public async Task<ActionResult<IList<RoomDto>>> GetAvailableRooms(
        [FromQuery] DateTime from,
        [FromQuery] DateTime to,
        [FromQuery] int? departmentId,
        [FromQuery] RoomType? roomType)
    {
        var query = new GetAvailableRoomsQuery
        {
            From = from,
            To = to,
            DepartmentId = departmentId,
            RoomType = roomType
        };

        var result = await Mediator.Send(query).ConfigureAwait(false);

        return Ok(result);
    }


    [HttpPost]
    public async Task<ActionResult<int>> Create(CreateRoomCommand command)
    {
        return await Mediator.Send(command).ConfigureAwait(false);
    }

    [HttpPut("{id:int}")]
    public async Task<ActionResult> Update(int id, UpdateRoomCommand command)
    {
        if (id != command.Id) return BadRequest();

        await Mediator.Send(command).ConfigureAwait(false);

        return NoContent();
    }

    [HttpDelete("{id:int}")]
    public async Task<ActionResult> Delete(int id)
    {
        await Mediator.Send(new DeleteRoomCommand {Id = id}).ConfigureAwait(false);

        return NoContent();
    }

    [HttpDelete("{ids}")]
    public async Task<ActionResult> Delete(int[] ids, DeleteRoomsCommand command)
    {
        await Mediator.Send(command).ConfigureAwait(false);

        return NoContent();
    }
}

【问题讨论】:

  • 你检查${this.actionUrl}?id=${ids.toString()}表达式的值是什么?
  • 9,10 转换为服务器上的数组...您可能想尝试像http://example.net/api/rooms?ids[0]=9&amp;ids[1]=10 这样构建您的 URL,首先从邮递员那里尝试这个,看看您是否能够调用 API。然后,您可以更改 Angular 代码以 tis 形式为多个 id 创建 URL。

标签: c# asp.net-core .net-core


【解决方案1】:

在get request中有两种方式使用多个id

  1. 路线值

你的网址应该是这样的(你可以用别的东西代替“,”)

http:\\....\deleteRooms\1,2,3,4

动作应该是这样的

[HttpGet("DeleteRooms/{ids}")] //or httpdelete
public ActionResult DeleteRooms(string ids)
{
  string[] roomIds = ids.split(",");
  ...
}
  1. 查询字符串
http:\\....\deleteRooms?ids=1&ids=2&ids=3&ids=4

动作可以是

[HttpGet("DeleteRooms")] //or httpdelete
public ActionResult DeleteRooms(int[] ids)
{
   ...
}

【讨论】:

  • 谢谢!我用了第二个,但你忘了[FromQuery]。 public async Task&lt;ActionResult&gt; Delete([FromQuery] int[] ids)
【解决方案2】:

如果您注意到,在您的 API 代码中,您需要一个整数,但您传递的是一个逗号分隔的数字(它是一个字符串)

[HttpDelete("{id:int}")]
public async Task<ActionResult> Delete(int id)
{

一种选择是将int 更改为string 并在控制器代码中进行字符串拆分。

public async Task<ActionResult> Delete(string ids)
{
  // split ids into array of int values. "1,2" into [1,2] using string.split

【讨论】:

猜你喜欢
  • 2016-11-19
  • 1970-01-01
  • 2021-02-11
  • 1970-01-01
  • 1970-01-01
  • 2022-01-04
  • 2023-02-23
  • 2013-01-30
  • 2012-07-27
相关资源
最近更新 更多