Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How To get Id from the url Path

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

my question is: how to get the ReceiptOrderNo(RO-20220731-4 in this case)?
I need it to fill "GetReceiptOrderByOrderNoAsync" method.

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading