Getting a partial URL From Request.Url

Even after almost 4 years working with ASP.NET, I can still never remember what property I need from Request.Url. So, as much to aid my own brain as yours, here's a handy-dandy table:

AbsolutePath/foo/bar/
AbsoluteUrihttp://www.foobar.com:8080/foo/bar/?foo=bar#foo
Authoritywww.foobar.com:8080
Fragment#foo
Hostwww.foobar.com
LocalPath/foo/bar/
OriginalStringhttp://www.foobar.com:8080/foo/bar/?foo=bar#foo
PathAndQuery/foo/bar/?foo=bar
Port8080
Query?foo=bar
Schemehttp

If you need the whole URL, except just some bit at the end, you can use GetLeftPart, with a value from the UriPartial enum. The value you pass, will be the last part of the URL included. For example, to remove the fragment from the URL above:

Request.Url.GetLeftPart(UriPartial.Query)

For what it's worth, Request.Url is just an Uri instance. If you'd like to learn more about this system class, please see the relevant MSDN documentation.

comments powered by Disqus