[英]Routing non-CRUD method to API Endpoint
請幫我完成我的家庭項目:我可以毫無問題地調用所有 CRUD 方法,但我不知道如何將非 CRUD API 調用定向到正確的端點。
試過這個:
var rentalEvent = rest.GetSingle<RentalEvent>($"api/rentalevent/boo/{licensePlate}");
要達到這一點:
[Route("/boo")]
[HttpGet("{licensePlate}")]
public RentalEvent GetGetRentalEventByLicensePlate(string licensePlate)
{
return new RentalEvent { RenterId = 88 }; //mock return class
}
Controller 類的屬性設置為:
[Route("api/[controller]")]
[ApiController]
HttpGet
中指定的路由覆蓋了您之前在Route
屬性中設置的內容。 嘗試以下方法之一:
僅對整個 Route 使用HttpGet
:
[HttpGet("boo/{licensePlate}")]
public RentalEvent GetGetRentalEventByLicensePlate(string licensePlate)
{
return new RentalEvent { RenterId = 88 }; //mock return class
}
或者使用不帶參數的HttpGet
並將整個路由放在Route
屬性中:
[Route("boo/{licensePlate}")]
[HttpGet]
public RentalEvent GetGetRentalEventByLicensePlate(string licensePlate)
{
return new RentalEvent { RenterId = 88 }; //mock return class
}
有關詳細信息,請參閱ASP.NET Web API 中的屬性路由。
最好只在一個地方定義 Api 路線。
[HttpGet("/boo")]
public RentalEvent GetGetRentalEventByLicensePlate(string licensePlate)
{
return new RentalEvent { RenterId = 88 }; //mock return class
}
對於 get 請求,通常應將字符串值用作查詢中的可選參數。
api/rentalevent/boo?licensePlate=mylicense
在路由定義中放置任意字符串意味着不同的端點而不是不同的參數值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.