Solved

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

Posted on 2013-05-14
3
1,804 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

914 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now