Solved

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

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

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Image(7) 1 36
Form design in vb.net 7 18
Get String split 5 33
Iterate a dictionnary to change values 4 28
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

706 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

19 Experts available now in Live!

Get 1:1 Help Now