Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get CreatedAtAction to add a query parameter?

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.

like image 758
s.m. Avatar asked Nov 21 '25 10:11

s.m.


1 Answers

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));
like image 126
Kirk Larkin Avatar answered Nov 22 '25 23:11

Kirk Larkin