ASP.NET Core Web API FromHeader binding x-api-key header

Viewed 1413

I have a x-api-key header that I want to bind to my controller paramter. I tried the below code but the parameter is still null.

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

After 5 minutes of posting this question I found the answer here. So using the name property of FromHeader.

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