Jak Mogę uzyskać podstawową wersję strony?
Chcę napisać małą metodę pomocniczą, która zwraca podstawowy adres URL witryny. Oto co wymyśliłem:
public static string GetSiteUrl()
{
string url = string.Empty;
HttpRequest request = HttpContext.Current.Request;
if (request.IsSecureConnection)
url = "https://";
else
url = "http://";
url += request["HTTP_HOST"] + "/";
return url;
}
Czy jest w tym jakiś błąd, który możesz wymyślić? Czy ktoś może to poprawić? 12 answers
Spróbuj tego:
string baseUrl = Request.Url.Scheme + "://" + Request.Url.Authority +
Request.ApplicationPath.TrimEnd('/') + "/";
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
2011-09-14 14:14:24
string baseUrl = Request.Url.GetLeftPart(UriPartial.Authority)
To jest to;)
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
2012-12-14 09:58:07
Popularne rozwiązanie GetLeftPart
nie jest niestety obsługiwane w wersji PCL Uri
. GetComponents
jest jednak, więc jeśli potrzebujesz przenośności, powinno to załatwić sprawę:
uri.GetComponents(
UriComponents.SchemeAndServer | UriComponents.UserInfo, UriFormat.Unescaped);
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-12-14 20:08:41
Uważam, że powyższe odpowiedzi nie biorą pod uwagę, gdy strona nie znajduje się w katalogu głównym witryny.
To jest kontroler dla WebApi:
string baseUrl = (Url.Request.RequestUri.GetComponents(
UriComponents.SchemeAndServer, UriFormat.Unescaped).TrimEnd('/')
+ HttpContext.Current.Request.ApplicationPath).TrimEnd('/') ;
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-05-02 20:06:38
Dla mnie @warlock wygląda na najlepszą odpowiedź, ale zawsze używałem tego w przeszłości;
string baseUrl = Request.Url.GetComponents(
UriComponents.SchemeAndServer, UriFormat.UriEscaped)
Lub w kontrolerze WebAPI;
string baseUrl = Url.Request.RequestUri.GetComponents(
UriComponents.SchemeAndServer, UriFormat.Unescaped)
Który jest przydatny, więc możesz wybrać, jaki format ucieczki chcesz. Nie wiem, dlaczego istnieją dwie tak różne implementacje i z tego, co wiem, ta metoda I @warlock zwracają dokładnie ten sam wynik w tym przypadku, ale wygląda na to, że GetLeftPart()
działa również dla znaczników Uri innych niż serwer, takich jak mailto
.
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-03-14 11:00:00
Jest to o wiele bardziej niezawodna metoda.
VirtualPathUtility.ToAbsolute("~/");
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-08-05 19:48:00
Idę z
HttpContext.Current.Request.ServerVariables["HTTP_HOST"]
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-02-18 06:20:03
Bazując na tym, co napisał Warlock, odkryłem, że virtual path root jest potrzebny, jeśli nie jesteś hostowany w katalogu głównym swojej sieci. (Działa to dla kontrolerów MVC Web API)
String baseUrl = Request.RequestUri.GetLeftPart(UriPartial.Authority)
+ Configuration.VirtualPathRoot;
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-03-23 12:55:03
Używam poniższego kodu z Application_Start
String baseUrl = Path.GetDirectoryName(HttpContext.Current.Request.Url.OriginalString);
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-04-28 15:19:07
To mi pasuje.
Request.Url.OriginalString.Replace(Request.Url.PathAndQuery, "") + Request.ApplicationPath;
- Prośba.Url.OriginalString : zwraca pełną ścieżkę taką samą, jaką wyświetla przeglądarka.
- Prośba.Url.PathAndQuery : zwraca (kompletną ścieżkę) - (nazwę domeny + PORT).
- Prośba.ApplicationPath : return " / "on hosted server and" application name " on local IIS deploy.
Więc jeśli chcesz uzyskać dostęp do nazwy domeny, rozważ podanie nazwy aplikacji w przypadku:
- IIS deployment
- Jeśli aplikacja została wdrożona w sub-domenie.
====================================
Dla dev.x.us/web
Zwraca to silny tekst
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
2020-01-27 09:12:33
Proszę użyć poniższego kodu
string.Format("{0}://{1}", Request.url.Scheme, Request.url.Host);
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
2019-03-19 11:43:27
Mógłbyś ewentualnie dodać port dla non port 80 / SSL?
Coś w stylu:
if (HttpContext.Current.Request.ServerVariables["SERVER_PORT"] != null && HttpContext.Current.Request.ServerVariables["SERVER_PORT"].ToString() != "80" && HttpContext.Current.Request.ServerVariables["SERVER_PORT"].ToString() != "443")
{
port = String.Concat(":", HttpContext.Current.Request.ServerVariables["SERVER_PORT"].ToString());
}
I użyć tego w wyniku końcowym?
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
2011-09-14 08:50:56