Solved

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

Posted on 2013-05-14
3
1,985 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.

751 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