I need to be able to get at the full URL of the page I am on from a user control. Is it just a matter of concatenating a bunch of Request variables together? If so which ones? Or is there a more simpiler way?
-
6To anyone in a loadbalanced webfarm.. beware the port number that appears when using System.Url: http://stackoverflow.com/questions/7674850/get-original-url-without-non-standard-port-c – felickz Nov 09 '12 at 20:25
10 Answers
Here is a list I normally refer to for this type of information:
Request.ApplicationPath : /virtual_dir
Request.CurrentExecutionFilePath : /virtual_dir/webapp/page.aspx
Request.FilePath : /virtual_dir/webapp/page.aspx
Request.Path : /virtual_dir/webapp/page.aspx
Request.PhysicalApplicationPath : d:\Inetpub\wwwroot\virtual_dir\
Request.QueryString : /virtual_dir/webapp/page.aspx?q=qvalue
Request.Url.AbsolutePath : /virtual_dir/webapp/page.aspx
Request.Url.AbsoluteUri : http://localhost:2000/virtual_dir/webapp/page.aspx?q=qvalue
Request.Url.Host : localhost
Request.Url.Authority : localhost:80
Request.Url.LocalPath : /virtual_dir/webapp/page.aspx
Request.Url.PathAndQuery : /virtual_dir/webapp/page.aspx?q=qvalue
Request.Url.Port : 80
Request.Url.Query : ?q=qvalue
Request.Url.Scheme : http
Request.Url.Segments : /
virtual_dir/
webapp/
page.aspx
Hopefully you will find this useful!

- 3,569
- 2
- 12
- 4
-
11) There is also Request.RawUrl; 2) Request.Url may throw an exception on 'very bad requested paths'. – user2864740 Jul 13 '17 at 04:35
-
Very helpful answer, but shouldn't the port numbers be consistent to avoid confusion (assuming all the examples are for the same request?e.g, Authority and Port imply port 80 but AbsoluteUri contains port 2000) – Young Bob Aug 14 '17 at 17:25
-
1
-
I usually use Request.Url.ToString()
to get the full url (including querystring), no concatenation required.

- 35,751
- 21
- 71
- 94
-
2If URL contain '#' then all the character after # will not be included. – Krunal Sisodiya Sep 01 '16 at 12:06
-
6@KrunalSisodiya I don't think that a URL's hash is ever sent to the server, but I could be wrong. – travis Sep 01 '16 at 17:21
-
2
Request.Url.AbsoluteUri
This property does everything you need, all in one succinct call.

- 2,577
- 13
- 26

- 39,797
- 30
- 87
- 118
-
6+1. This is the best answer. Should be preferred over Request.Url.ToString(). – Todd Menier Sep 04 '13 at 12:49
For ASP.NET Core
you'll need to spell it out:
var request = Context.Request;
@($"{ request.Scheme }://{ request.Host }{ request.Path }{ request.QueryString }")
Or you can add a using statement to your view:
@using Microsoft.AspNetCore.Http.Extensions
then
@Context.Request.GetDisplayUrl()
The _ViewImports.cshtml
might be a better place for that @using

- 28,927
- 17
- 154
- 183
-
1
-
1That should probably be `$"{ request.Scheme }{ Uri.SchemeDelimiter }{ request.Host }{ request.PathBase.Add(request.Path) }{ request.QueryString }"` – Jeremy Lakeman Apr 28 '21 at 07:07
-
Probably? You should test such assumptions before commenting as such. I tested mine... – Serj Sagan Apr 28 '21 at 08:05
if you need the full URL as everything from the http to the querystring you will need to concatenate the following variables
Request.ServerVariables("HTTPS") // to check if it's HTTP or HTTPS
Request.ServerVariables("SERVER_NAME")
Request.ServerVariables("SCRIPT_NAME")
Request.ServerVariables("QUERY_STRING")

- 8,275
- 4
- 40
- 63
-
2This doesn't take into account non-standard ports (i.e. not using port 80 for http) – Spongeboy Aug 15 '11 at 03:01
-
1you could use Request.ServerVariables("SERVER_PORT") for port detection ! – armen Oct 31 '14 at 09:00
-
1Note that this answer uses VB.Net syntax. In C#, use Request.ServerVariables["HTTPS"] for example. – jaycer May 04 '17 at 15:03
Request.RawUrl

- 172,459
- 74
- 246
- 311
-
7this doesn't not return the full url. If the full url is http://www.blah.com/1.1/home/main?a=1 Request.RawUrl will return 1.1/hom/main?a=1 – jnoreiga Aug 02 '12 at 18:06
Better to use Request.Url.OriginalString
than Request.Url.ToString()
(according to MSDN)
-
-
@DirkThe from link to MSDN above: "String returned by the ToString method may contain control characters, which can corrupt the state of a console application. You can use the GetComponents method with the UriFormat.SafeUnescaped format to remove control characters from the returned string." – Artem Mar 13 '14 at 10:44
-
Thanks guys, I used a combination of both your answers @Christian and @Jonathan for my specific need.
"http://" + Request.ServerVariables["SERVER_NAME"] + Request.RawUrl.ToString()
I don't need to worry about secure http, needed the servername variable and the RawUrl handles the path from the domain name and includes the querystring if present.

- 10,379
- 12
- 49
- 68
-
2You should also know that RawUrl, unlike Request.Url, represents the original, unmapped request url if url mapping is being used. – harpo Sep 09 '08 at 05:44
-
SERVER_NAME often does not return the "real" URL used by the client. HTTP_HOST is probably the better form if going to dig in directly.. also misses out simple HTTP-to-another-port. – user2864740 Jul 13 '17 at 04:39
If you need the port number also, you can use
Request.Url.Authority
Example:
string url = Request.Url.Authority + HttpContext.Current.Request.RawUrl.ToString();
if (Request.ServerVariables["HTTPS"] == "on")
{
url = "https://" + url;
}
else
{
url = "http://" + url;
}

- 11
- 6
Try the following -
var FullUrl = Request.Url.AbsolutePath.ToString();
var ID = FullUrl.Split('/').Last();

- 3,737
- 4
- 33
- 56

- 418
- 4
- 6