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/ |
---|---|
AbsoluteUri | http://www.foobar.com:8080/foo/bar/?foo=bar#foo |
Authority | www.foobar.com:8080 |
Fragment | #foo |
Host | www.foobar.com |
LocalPath | /foo/bar/ |
OriginalString | http://www.foobar.com:8080/foo/bar/?foo=bar#foo |
PathAndQuery | /foo/bar/?foo=bar |
Port | 8080 |
Query | ?foo=bar |
Scheme | http |
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.