O'Reilly logo

ASP.NET in a Nutshell by Matthew MacDonald, G. Andrew Duthie

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Name

ServerVariables

Synopsis

NameValueCollection 
                      = Request.ServerVariables

Parameters

NameValueCollection

An Object variable of type NameValueCollection.

Example

The example, as in the previous collection-related examples, writes the contents of the ServerVariables collection to the browser:

Sub Page_Load(  )
   Dim Counter1, Counter2 As Integer
   Dim Keys(), subKeys(  ) As String
   Dim SVarsColl As NameValueCollection
  
   ' Load ServerVariables into NameValueCollection
   SVarsColl=Request.ServerVariables
   ' Get keys into an array
   Keys = SVarsColl.AllKeys
   For Counter1 = 0 To Keys.GetUpperBound(0)
      Message.Text &= "Key: " & Keys(Counter1) & "<br/>"
      subKeys = SVarsColl.GetValues(Counter1) 
      ' Get all values under this key
      For Counter2 = 0 To subKeys.GetUpperBound(0)
         Message.Text &= "Value " & CStr(Counter2) & ": " & _
            subKeys(Counter2) & "<br/>"
      Next Counter2
      Message.Text &= "<br/>"
   Next Counter1
End Sub

Notes

In addition to retrieving all the values by looping through the Keys, you can access individual values if you know their key. The following list shows the available keys for the ServerVariable collection:

ALL_HTTP

Returns a string containing all HTTP headers with each header name taking the form HTTP_ headername, for which headername is the name of an HTTP header in all capital letters.

ALL_RAW

Provides the same information as ALL_HTTP, but header names are not all capital letters and are not prefixed with HTTP_.

APPL_MD_PATH

Returns the path of the application in the IIS metabase.

APPL_PHYSICAL_PATH ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required