I see a lot of ASP.NET programmers still using the Request.ServerVariables collection to get specific information they need like url paths, is https active, or the full query string. It’s time to move on and start using the Request object as it should be used.

Below are some examples of the old way using ServerVariables and the new way:

Current URL Example: http://www.imulus.com:8080/test/hello.aspx?test=yes 

OLD Request.ServerVariables["URL"]; Output: /test/hello.aspx

NEW Request.Url.AbsolutePath; Output: /test/hello.aspx

You can also get any part of the url you want. Some examples:

Request.Url.AbsoluteUri; Output: http://www.imulus.com:8080/test/hello.aspx?test=yes Request.Url.Authority;

Output: www.imulus.com:8080 Request.Url.DnsSafeHost; 

Output: www.imulus.com Request.Url.Host; 

Output: www.imulus.com Request.Url.PathAndQuery; 

Output: /test/hello.aspx?test=yes Request.Url.Port; 

Output: 8080 Request.Url.Segments; Output: {"/","test/","hello.aspx"}

… and much more!

Headers in raw form

OLD Request.ServerVariables["ALL_RAW"];NEW Request.Headers.ToString();

Headers is a collection so you can do this:

Request.Headers.Get("Connection")

Is SSL/HTTPS Active
OLD
Request.ServerVariables["HTTPS"];
(Returns "ON" or "OFF")

NEW
Request.IsSecureConnection;
(Returns boolean: true or false)

Query String
OLD
Request.ServerVariables["QUERY_STRING"];
NEW
Request.QueryString.ToString()

QueryString is a collection so for a query string “?test=false&hello=you” you can do this:

Request.QueryString.Get("test")

Browser User Agent
>OLD
Request.ServerVariables["HTTP_USER_AGENT"];NEW
Request.UserAgent;

User's IP Address accessing the site
OLD
Request.ServerVariables["REMOTE_ADDR"];NEW
Request.UserHostAddress;

These are just a few examples on the information you can get your hands on without using Server Variables.