ASP.NET Request object cheat sheet

johna by | August 8, 2014 | ASP.NET Web Forms Web Development

Personally I can never remember which of the Request object properties I need to use to get the requested URL or certain parts of the requested URL.

This cheat sheet lists the commonly used properties for this purpose, a description, and some examples of what possible values to expect.

Request.ApplicationPath
Returns the path relative to the website root.
/

Request.CurrentExecutionFilePath
Returns the path relative to the website root.
/test.aspx

Request.CurrentExecutionFilePathExtension
Returns the file extension of the requested page.
.aspx

Request.FilePath
Returns the path without querystring.
/test.aspx

Request.Path
Returns the full path without querystring.
/test.aspx

Request.RawUrl
Returns the full path and querystring.
/test.aspx?key=value

Request.PhysicalApplicationPath
Returns the file location of the website directory.
c:\wwwroot\

Request.PhysicalPath
Returns the page file name including file location.
c:\wwwroot\test.aspx

Request.QueryString
Returns the querystring without the leading question mark. If no querystring then returns an empty string.
key=value

Request.RequestType
Returns the request method as a string.
GET
POST


Request.Url
Returns the full URL including protocol, domain name, port (only if not 80), full path and querystring.
http://domain.com/test.aspx?key=value
http://domain.com:61615/Test.aspx?key=value

Request.Url.AbsolutePath
Returns the full path.
/test.aspx

Request.Url.AbsoluteUri
Returns the the full URL including protocol, domain name, port (only if not 80), full path and querystring.
http://domain.com/test.aspx?key=value
http://domain.com:61615/Test.aspx?key=value


Request.Url.Host
Returns only the host name (domain name including any requested sub-domain)
www.domain.com
my.test.domain.com

Request.Url.OriginalString
Returns the full URL including protocol, domain name, port, full path and querystring.
http://domain.com:80/test.aspx?key=value

Request.Url.PathAndQuery
Returns the full path and querystring.
/test.aspx?key=value

Request.Url.Query
Returns the querystring portion only, including the leading question mark. If there is no querystring then it returns an empty string.
?key=value

Request.Url.Scheme
Returns the protocol as a string.
http
https


Request.Url.Segements
Returns a string array of the path split by forward slashes.
{ /, folder/, test.aspx }

Important note regarding default pages

If the default page is requested without using the page name (ie. just the domain name or folder name) all of these methods except Request.RawUrl will still return the default page's name. If you need to know exactly what was requested use Request.RawUrl.

Important notes regarding querystrings

If the querystring is blank but a question mark was provided, all of these methods except Request.RawUrl will not include the question mark. If you need to know exactly what was requested use Request.RawUrl.

If a querystring key is requested but without equals sign and value, then all methods return the querystring exactly as entered, eg. ?key1=value&key2&key3=value.

Important note regarding upper and lower case in requests

Host names are always returned in lower case, regardless of how entered.

All methods return the case exactly as entered for the path and querystring, regardless of the actual case used for the physical folder or file name.

Related Posts

Web Development

How to set up a debugging using the Turnkey Linux LAMP stack and VS Code

by johna | December 19, 2023
The second part in my guide to setting up a website and database using the Turnkey Linux LAMP stack.

Website Hosting Web Development

How to set up a website and database using the Turnkey Linux LAMP stack

by johna | November 18, 2023
If you need to host your own website for the purposes of web development, Turnkey Linux LAMP Stack is an easy to install all-in-one solution that you can set up on a spare computer or a VM (Virtual Machine).

Web Development

Intermittent "Unable to read data from the transport connection: net_io_connectionclosed" errors

by johna | May 6, 2020
If you are having intermittent problems sending email in .NET using System.Net.Mail consider switching libraries.

Comments

There are no comments yet. Be the first to leave a comment!

Leave a Comment

About

...random postings about web development and programming, Internet, computers and electronics topics.

I recommend ASPnix for web hosting and Crazy Domains for domain registration.

Subscribe

Get the latest posts delivered to your inbox.