PASSING A PARAMETER FROM REQUEST HEADER TO THE API CONTROLLER ACTION METHOD

john rob

Total Post:108

Points:756
Posted by  john rob
 843  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. Sumit Kesarwani

    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();

Answer

NEWSLETTER

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