ASP.NET Core Web API FromHeader 绑定 x-api-key 标头

Rod*_*ing 4 http-headers asp.net-core asp.net-core-webapi

我有一个x-api-key标头想要绑定到我的控制器参数。我尝试了下面的代码,但参数仍然为空。

[HttpGet]
public ActionResult Get([FromHeader] xApiKey) {
    var apikey = xApiKey;
}
Run Code Online (Sandbox Code Playgroud)

Rod*_*ing 6

发布这个问题 5 分钟后,我在这里找到了答案。所以使用nameFromHeader的属性。

[HttpGet]
public ActionResult Get([FromHeader(Name = "x-api-key")] apiKey) {
     // code here
}
Run Code Online (Sandbox Code Playgroud)