首页 > 解决方案 > ASP.NET Core Web API FromHeader 绑定 x-api-key 标头

问题描述

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

[HttpGet]
public ActionResult Get([FromHeader] xApiKey) {
    var apikey = xApiKey;
}

标签: asp.net-corehttp-headersasp.net-core-webapi

解决方案


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

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

推荐阅读