Is there a way to have CreatedAtAction append a query parameter to the Location header that gets generated?
The action method I'm using is declared as such:
[HttpGet("{candidateId:guid}")]
public async Task<ActionResult> Get(Guid candidateId, [FromQuery][Required]string siteId)
and I'm pointing to it when calling CreatedAtAction:
var model = RegisterModel(/* ... */);
return CreatedAtAction(nameof(Get), new { candidateId = model.CandidateId }, model));
siteId is absolutely required for this action method to work, and that's why I would like to include it in the URL returned in the Location header: I would like my URL to be a working one.
You can add a siteId property to the anonymous object that you're creating - anything that's not specified in the route itself is set automatically as a query-string parameter:
return CreatedAtAction(
nameof(Get),
new { candidateId = model.CandidateId, siteId = model.SiteId },
model));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With