Solved

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

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

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 …
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

770 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