Giobbo
Giobbo

Reputation: 249

ASP.NET How get a custom header in a Request?

I have a simple question. I set a custom header like this:

var client = new HttpClient();
string urlWebApi = "urlOfMyApi";
client.BaseAddress = new Uri(urlWebApi);
client.DefaultRequestHeaders.Accept.Clear();
string token = "valueOfToken";
client.DefaultRequestHeaders.Add("Token", token);

How get the value of header "Token" in my Api?

Upvotes: 3

Views: 1719

Answers (1)

Theo
Theo

Reputation: 885

In your API Controller, you should be able to access the HttpRequest which has a "Headers" property which is a collection of Key/Value pairs, so you can get it like this:

public HttpResponseMessage SomeControllerAction()
{
    return Request.Headers["Token"]
}

Upvotes: 4

Related Questions