Getting current page URL (or it’s parts) in C#

Here is an easy reference for how to get different parts of an URL in C#.

I’ve color-coded the example URL below for easier matching with the values of the properties of the HttpRequest.

URL used for this example:
http://localhost:12345/site/page.aspx?q1=1&q2=2

Value of HttpContext.Current.Request.Url.Host
localhost

Value of HttpContext.Current.Request.Url.Authority
localhost:12345

Value of HttpContext.Current.Request.Url.AbsolutePath
/site/page.aspx

Value of HttpContext.Current.Request.ApplicationPath
/site

Value of HttpContext.Current.Request.Url.AbsoluteUri
http://localhost:12345/site/page.aspx?q1=1&q2=2

Value of HttpContext.Current.Request.RawUrl
/site/page.aspx?q1=1&q2=2

Value of HttpContext.Current.Request.Url.PathAndQuery
/site/page.aspx?q1=1&q2=2