Sunday, May 22, 2011

Request.ServerVariables, Another LINQ way

I recently came across this blog, by using an extension method with sample codes on how to retrieve the value from Request.ServerVariables using LINQ.

Here is my modification of the method, which is much more shorter, using LINQ to to retrieve the server variables:

Dim d As IEnumerable(Of System.Collections.Generic.KeyValuePair(Of String, String)) = _
From c In System.Web.HttpContext.Current.Request.ServerVariables _
Let s = System.Web.HttpContext.Current.Request.ServerVariables(c) _
Select New System.Collections.Generic.KeyValuePair(Of String, String)(c, s)

After that, bind it to GridView:

GridView1.DataSource = d
GridView1.Bind()

This will give you all the values from Request.ServerVariables.

Request.ServerVariables, Another LINQ way | My Code Snippet