My question may look simple but I'm stuck in this problem for a while.
Code Controller:
[Route("api/ReceiptOrders/{receiptOrderNo}/[controller]")]
[ApiController]
public class ReceiptPositionsController : ControllerBase
{
[HttpPost("{orderNo}")]
public async Task<IActionResult> PostReceiptOrderPositions(
[FromBody] IEnumerable<ReceiptPositionForCreationDto> receiptPositions)
{
var receiptOrder = await _repositoryManager.ReceiptOrder.GetReceiptOrderByOrderNoAsync(receiptOrderNo, false);
//More code here...
}
}
URL: https://localhost:7164/api/ReceiptOrders/RO-20220731-4/ReceiptPositions
my question is: how to get the ReceiptOrderNo(RO-20220731-4 in this case)? I need it to fill "GetReceiptOrderByOrderNoAsync" method.
CodePudding user response:
Bind to the template entry by adding another parameter in your method. In addition, remove the template from the method's attribute. A method's template is appended to the class's template, so what you currently have won't match.
[Route("api/ReceiptOrders/{receiptOrderNo}/[controller]")]
[ApiController]
public class ReceiptPositionsController : ControllerBase
{
[HttpPost] // <--- no template
public async Task<IActionResult> PostReceiptOrderPositions(
[FromRoute] string receiptOrderNo, // <---- new parameter
[FromBody] IEnumerable<ReceiptPositionForCreationDto> receiptPositions)
{
// ... use "receiptOrderNo"
}
You don't need [FromRoute]
, but I think it's a good idea as it makes it explicit where the value should be read from, instead of accidently read from a Query String or another source.