請幫助我完成我的家庭專案:我可以毫無問題地呼叫所有 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
}
控制器類的屬性設定為:
[Route("api/[controller]")]
[ApiController]
uj5u.com熱心網友回復:
HttpGetwith 中指定的路由會覆寫您之前在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 中的屬性路由。
uj5u.com熱心網友回復:
最好只在一個地方定義 Api 路由。
[HttpGet("/boo")]
public RentalEvent GetGetRentalEventByLicensePlate(string licensePlate)
{
return new RentalEvent { RenterId = 88 }; //mock return class
}
對于獲取請求,通常應將字串值用作查詢中的可選引數。
api/rentalevent/boo?licensePlate=mylicense
在路由定義中放置任意字串意味著不同的端點而不是不同的引數值。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/376746.html
上一篇:C#將SQL輸出顯示到文本框中
下一篇:從輸入模型C#分配動態變數
