devxlogo

Avoid querying the ServerVariables collection

Avoid querying the ServerVariables collection

You can access a lot of useful information through the ServerVariables collection, but this has a price in terms of performance. More precisely, the first time you reference this collection in a page, IIS must collect all the data that is necessary to create the collection. All the subsequent references come for free, however.

In some cases you can substitute a reference to a server variable with a constant value. For example, many developers use the LOCAL_ADDR, SERVER_PORT, and PATH_INFO server variables to dynamically build a path relative to the current page or the domain that hosts the page:

fullurl = "http://" & Request.ServerVariables("LOCAL_ADDR")    & ":" & Request.ServerVariables("SERVER_PORT")   & Request.ServerVariables("PATH_INFO")

However, these values vary infrequently, so you should consider using constant strings instead. If you want to build a page that works correctly regardless of the domain it runs under (which is especially useful to debug the page before uploading to the real Internet server) you can store the domain path in an Application variable in the Application_OnStart procedure in GLOBAL.ASA.

See also  Professionalism Starts in Your Inbox: Keys to Presenting Your Best Self in Email
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist