'Bind [FromRoute] and [FromBody] into one model
I have this endpoint
[HttpPost]
[Route("company/{companyid}/bankaccount")]
public async Task<Unit> AddCompanyBankAccount([FromBody]AddCompanyBankAccountCommand request)
{
return await _mediator.Send(request);
}
And model
public class AddCompanyBankAccountCommand : IRequest<Unit>
{
public Guid CompanyId { get; set; }
public string BankName { get; set; } = String.Empty;
public string AccountNo { get; set; } = String.Empty;
public Guid CurrencyId { get; set; }
}
I would like my endpoint to be something like POST contoso.com/company/{companyid}/bankaccount and I want to bind the {companyid} as Guid from route to the model. Is this possible WITHOUT custom model binding? I'm using NET 6.0
I also saw this website, but it's not working for me
Solution 1:[1]
I try the code and webside you provide, Because the type of CompanyId is Guid, So When I post xxxx/company/8719AEDB-D34A-DAA3-E0C0-BA307F48F349/bankaccount, As the website'method show, It can bind normally.
But, in your question, When you post xxxxx/company/123/bankaccount, you want bind 123 from route to the model, Because the type of 123 is int, the type of CompanyId is Guid, Their types are differnet and You cannot convert Guid to int, So you can't bind them.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Xinran Shen |

