Passing a parameter from request header to the API controller action method

Total Post:108

Points:756
 1059  View(s)
Ratings:
Rate this:

I am using WEB API 2.0 for REST Service development and I need to pass a parameter from request header to the API controller action method. How can I? 

By default API controller is reading the parameters from request body. How can I make it read parameter from request header? 

[HttpPost]
[Route("abc")]
public IHttpActionResult abcMethod(string s)
{
   //some code
}

I want the above abcMethod to read it's parameter from request header.

Pls suggest.

  1. Post:378

    Points:2694
    Re: Passing a parameter from request header to the API controller action method

    Hi John, 

    try this:

    IEnumerable<string> headerValues =request.Headers.GetValues("MyCustomID");
    var id = headerValues.FirstOrDefault();

      Modified On Apr-06-2018 06:40:21 AM

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!