HttpRequest..::.ServerVariables Property
This page is specific to:.NET Framework Version:
.NET Framework Class Library
HttpRequest..::.ServerVariables Property

Gets a collection of Web server variables.

Namespace:  System.Web
Assembly:  System.Web (in System.Web.dll)
Syntax

'Usage

Dim instance As HttpRequest
Dim value As NameValueCollection

value = instance.ServerVariables


'Declaration

Public ReadOnly Property ServerVariables As NameValueCollection
Remarks

For a list of server variables supported by IIS, see IIS Server Variables.

Examples

The following code example displays the names and values of all named server variables.

Dim loop1, loop2 As Integer
Dim arr1(), arr2() As String
Dim coll As NameValueCollection

' Load ServerVariable collection into NameValueCollection object.
coll=Request.ServerVariables 
' Get names of all keys into a string array.
arr1 = coll.AllKeys 
For loop1 = 0 To arr1.GetUpperBound(0)
   Response.Write("Key: " & arr1(loop1) & "<br>")
   arr2 = coll.GetValues(loop1) ' Get all values under this key.
   For loop2 = 0 To arr2.GetUpperBound(0)
      Response.Write("Value " & CStr(loop2) & ": " & Server.HtmlEncode(arr2(loop2)) & "<br>")
   Next loop2
Next loop1



.NET Framework Security

Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
See Also

Reference

Community Content

AllKeys v/s Keys?
Added by:Vivek Athalye

Hi,

I was trying to iterate on the ServerVariables collection with the following block of code (C#):

foreach (string name in coll.Keys) {...}


In this case I was consistantly getting "Collection was modified after the enumerator was instantiated" error.
Instead of Keys when I use AllKeys the error disappears.

Similerly, if I use for loop instead of foreach as follows, the error disappears:

    for(int i=0; i<coll.Keys.Count; i++) {...}


For that matter, if I just access the Keys.Count property once, before the first foreach loop, then also the code works without any error. I was not clear why this kind of behavior is there. Hence I came here to see if there are any remarks and what I found is that the sample code given above is also using AllKeys instead of Keys. Can someone explain why am I getting "Collection was modified" error in first place?

© 2010 Microsoft Corporation. All rights reserved.   Terms of Use | Trademarks | Privacy Statement
Page view tracker
Rate the Lightweight library
x
Lightweight builds on ScriptFree (loband) by adding features you've requested: a SearchBox and default code language selection.
Do you like the SearchBox?
Do you like the tabbed code blocks?
How useful is this topic?
Tell us more.
Thanks
x
You're helping to improve MSDN Online.
Feedback
Switch View
Classic
Lightweight Beta
ScriptFree
Switch View