我正在嘗試創建一個應該洗掉多個 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();
}
}
uj5u.com熱心網友回復:
有兩種方法可以在 get 請求中使用多個 id
- 路由值
你的網址應該是這樣的(你可以用別的東西代替“,”)
http:\\....\deleteRooms\1,2,3,4
動作應該是這樣的
[HttpGet("DeleteRooms/{ids}")] //or httpdelete
public ActionResult DeleteRooms(string ids)
{
string[] roomIds = ids.split(",");
...
}
- 請求引數
http:\\....\deleteRooms?ids=1&ids=2&ids=3&ids=4
動作可以是
[HttpGet("DeleteRooms")] //or httpdelete
public ActionResult DeleteRooms(int[] ids)
{
...
}
uj5u.com熱心網友回復:
如果您注意到,在您的 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
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/367633.html
