Jak uzyskać pełny adres url strony, na której jestem w C#

Muszę być w stanie uzyskać pełny adres URL strony, na której jestem z kontroli użytkownika. Czy to tylko kwestia połączenia kilku zmiennych żądań? Jeśli tak, które? A może jest bardziej prosty sposób?

Author: travis, 2008-09-03

9 answers

Zwykle używam Request.Url.ToString(), aby uzyskać pełny adres url (w tym querystring), nie jest wymagana konkatenacja.

 138
Author: travis,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2008-09-03 03:49:18

Oto lista, do której zwykle odwołuję się dla tego typu informacji:

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

Mam nadzieję, że okaże się to przydatne!

 293
Author: Mohsen,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-01-20 04:46:29
Request.Url.AbsoluteUri
Ta nieruchomość robi wszystko, czego potrzebujesz, wszystko w jednym zwięzłym telefonie.
 64
Author: DevelopingChris,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-05-03 13:03:51

Prośba.RawUrl

 9
Author: FlySwat,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2008-09-02 21:11:23

Jeśli potrzebujesz pełnego adresu URL jako wszystkiego od http do querystring będziesz musiał połączyć następujące zmienne

Request.ServerVariables("HTTPS") // to check if it's HTTP or HTTPS
Request.ServerVariables("SERVER_NAME") 
Request.ServerVariables("SCRIPT_NAME") 
Request.ServerVariables("QUERY_STRING")
 9
Author: Christian Hagelid,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2008-09-02 21:16:36

Dzięki chłopaki, użyłem kombinacji obu twoich odpowiedzi @ Christian i @ Jonathan dla mojej konkretnej potrzeby.

"http://" + Request.ServerVariables["SERVER_NAME"] +  Request.RawUrl.ToString()

Nie muszę się martwić o Bezpieczny http, potrzebna jest zmienna servername, a RawUrl obsługuje ścieżkę z nazwy domeny i zawiera querystring, jeśli istnieje.

 5
Author: RedWolves,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2008-09-02 21:27:38

Lepiej używać Request.Url.OriginalString than Request.Url.ToString() (zgodnie z MSDN )

 5
Author: Eni,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-10-19 06:16:03

Dla ASP.NET Core Musisz to przeliterować:

@($"{Context.Request.Scheme}://{Context.Request.Host}{Context.Request.Path}{Context.Request.QueryString}")

Lub możesz dodać do widoku instrukcję using:

@using Microsoft.AspNetCore.Http.Extensions

Then

@Context.Request.GetDisplayUrl()

_ViewImports.cshtml może być lepsze miejsce na to @using

 2
Author: Serj Sagan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-09-20 00:42:32

Jeśli potrzebujesz również numeru portu, możesz użyć

Request.Url.Authority

Przykład:

string url = Request.Url.Authority + HttpContext.Current.Request.RawUrl.ToString();

if (Request.ServerVariables["HTTPS"] == "on")
{
    url = "https://" + url;
}
else 
{
    url = "http://" + url;
}
 1
Author: IonB,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-12-03 14:17:14