[英]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.