Solved

Get multiple http header values (Request.Headers.TryGetValues)

Posted on 2013-05-14
3
1,933 Views
Last Modified: 2013-05-21
In my ASP.NET Web API I need to get up to 10 different http header values. I found this example that shows how to get one single http header value:

IEnumerable<string> values = new List<string>();
Request.Headers.TryGetValues("Test1", out values);
var test1 = values.First();

Open in new window


If I also need to get header values "Test2", "Test3", "Test4", "Test5" and so on, how can this be done? I guess there is a bad solution to repeat the code above 10 times...
0
Comment
Question by:webressurs
3 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 39164122
Encapsulate that bit of logic into a function that you can call successively.

e.g.

private string GetTestHeader(int value)
{
    IEnumerable<string> values = new List<string>();

    Request.Headers.TryGetValues("Test" + value.ToString(), out values);

    return values.FirstOrDefault() ?? string.Empty;
}

Open in new window


Then you can do something like:

var test1 = GetTestHeader(1);
var test2 = GetTestHeader(2);
var test3 = GetTestHeader(3);
var test4 = GetTestHeader(4);
var test5 = GetTestHeader(5);
var test6 = GetTestHeader(6);
var test7 = GetTestHeader(7);
var test8 = GetTestHeader(8);
var test9 = GetTestHeader(9);
var test10 = GetTestHeader(10);

Open in new window

0
 
LVL 5

Expert Comment

by:milindsaraswala
ID: 39164990
You can do something like this

WebHeaderCollection header = request.Headers;
for (int i = 0; i < header.Count; i++)
Console.WriteLine("   {0} : {1}",header.GetKey(i), header[i]);

Open in new window

0
 
LVL 1

Author Comment

by:webressurs
ID: 39167380
This line:
WebHeaderCollection header = request.Headers;

Gives this error:
Cannot implicitly convert type 'System.Net.Http.Headers.HttpRequestHeaders' to 'System.Net.WebHeaderCollection'

I got a solution that works fine, here is the code:

        private string GetHeader(string key)
        {
            var header = "";
            if (Request.Headers.Contains(key))
            {
                IEnumerable<string> values = new List<string>();
                Request.Headers.TryGetValues(key, out values);
                if (!string.IsNullOrEmpty(values.First())) header = values.First();
            }
            return header;
        }

        public IEnumerable<API.Models.UserCard> GetUsers()
        {
            var headerTest1 = GetHeader("Test1");
            var headerTest2 = GetHeader("Test2");
            var headerTest3 = GetHeader("Test3");

            var Test1 = headerTest1 != "" ? int.Parse(headerTest1) : 0;
            var Test2 = headerTest1 != "" ? int.Parse(headerTest2) : 0;
            var Test3 = headerTest1 != "" ? int.Parse(headerTest3) : 0;
        .
        .
        .
        }

My code is almost identical to kaufmed example above, hope this is a good solution :)
0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question