如何让 CreatedAtAction 添加查询参数?

s.m*_*.m. 2 c# asp.net-core

有没有办法CreatedAtAction将查询参数附加到Location生成的标头?

我正在使用的操作方法声明如下:

[HttpGet("{candidateId:guid}")]
public async Task<ActionResult> Get(Guid candidateId, [FromQuery][Required]string siteId)
Run Code Online (Sandbox Code Playgroud)

我在打电话时指着它CreatedAtAction

var model = RegisterModel(/* ... */);

return CreatedAtAction(nameof(Get), new { candidateId = model.CandidateId }, model));
Run Code Online (Sandbox Code Playgroud)

siteId绝对需要此操作方法工作,这就是为什么我想将它包含在Location标题中返回的 URL 中:我希望我的 URL 能够正常工作。

Kir*_*kin 5

您可以向siteId正在创建的匿名对象添加属性 - 路由本身中未指定的任何内容都会自动设置为查询字符串参数:

return CreatedAtAction(
    nameof(Get),
    new { candidateId = model.CandidateId, siteId = model.SiteId },
    model));
Run Code Online (Sandbox Code Playgroud)