0

I'm trying to create an endpoint supposed to delete multiple ids. It should match deleteRoom(ids: number[]). You can see what I tried below but it doesn't match the request from the 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();
    }
}
3
  • Did you check what is the value of ${this.actionUrl}?id=${ids.toString()} expression? Commented Nov 26, 2021 at 22:55
  • @Chetan, ` localhost:5001/api/Rooms?id=9,10` Commented Nov 26, 2021 at 22:58
  • 9,10 translate to an array at server... you might want to try building your URL like http://example.net/api/rooms?ids[0]=9&ids[1]=10 Try this first from the post man and see if you are able to call the API. Then you can change the angular code to create URL in tis form for multiple ids. Commented Nov 26, 2021 at 23:07

2 Answers 2

1

there two ways to use several ids in get request

  1. Route values

your url should be like this ( you can use something else instead of ",")

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

the action should be like this

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

the action can be

[HttpGet("DeleteRooms")] //or httpdelete
public ActionResult DeleteRooms(int[] ids)
{
   ...
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I used the second one, but you forgot [FromQuery]. public async Task<ActionResult> Delete([FromQuery] int[] ids)
1

If you notice, in your API Code, you are expecting a single integer but what you are passing is a comma separated numbers (which is a string)

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

One option is to change int to string and do a string split within controller code.

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

1 Comment

ids = null. This is the URL localhost:5001/api/Rooms?id=9,10 that the frontend is trying to send to. Also note that I could send only localhost:5001/api/Rooms?id=9

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.