ASP.NET에서 루트 도메인 URI를 얻으려면 어떻게해야합니까?
http://www.foobar.com 에서 웹 사이트를 호스팅한다고 가정 해 보겠습니다 .
내 코드 뒤에있는 " http://www.foobar.com/ "을 프로그래밍 방식으로 확인할 수있는 방법이 있습니까 (예 : 웹 구성에서 하드 코딩하지 않고도)?
HttpContext.Current.Request.Url 은 URL에 대한 모든 정보를 얻을 수 있습니다. 그리고 URL을 조각으로 나눌 수 있습니다.
string baseUrl = Request.Url.GetLeftPart(UriPartial.Authority);
GetLeftPart 메서드는 URI 문자열의 가장 왼쪽 부분이 포함 된 문자열을 반환하며 part로 지정된 부분으로 끝납니다.
URI의 체계 및 권한 세그먼트.
여전히 궁금한 사람은 http://devio.wordpress.com/2009/10/19/get-absolut-url-of-asp-net-application/ 에서 더 완전한 답변을 얻을 수 있습니다 .
public string FullyQualifiedApplicationPath
{
get
{
//Return variable declaration
var appPath = string.Empty;
//Getting the current context of HTTP request
var context = HttpContext.Current;
//Checking the current context content
if (context != null)
{
//Formatting the fully qualified website url/name
appPath = string.Format("{0}://{1}{2}{3}",
context.Request.Url.Scheme,
context.Request.Url.Host,
context.Request.Url.Port == 80
? string.Empty
: ":" + context.Request.Url.Port,
context.Request.ApplicationPath);
}
if (!appPath.EndsWith("/"))
appPath += "/";
return appPath;
}
}
string hostUrl = Request.Url.Scheme + "://" + Request.Url.Host; //should be "http://hostnamehere.com"
예제 URL이 http://www.foobar.com/Page1 인 경우
HttpContext.Current.Request.Url; //returns "http://www.foobar.com/Page1"
HttpContext.Current.Request.Url.Host; //returns "www.foobar.com"
HttpContext.Current.Request.Url.Scheme; //returns "http/https"
HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority); //returns "http://www.foobar.com"
전체 요청 URL 문자열을 얻으려면 :
HttpContext.Current.Request.Url
요청의 www.foo.com 부분을 얻으려면 :
HttpContext.Current.Request.Url.Host
어느 정도는 ASP.NET 응용 프로그램 외부의 요인에 따라 달라집니다. IIS가 응용 프로그램에 대해 여러 또는 임의의 호스트 헤더를 허용하도록 구성된 경우 DNS를 통해 응용 프로그램으로 확인 된 모든 도메인은 사용자가 입력 한 URL에 따라 요청 URL로 표시 될 수 있습니다.
-포트를 추가하면 IIS Express를 실행할 때 도움이 될 수 있습니다.
Request.Url.Scheme + "://" + Request.Url.Host + ":" + Request.Url.Port
string domainName = Request.Url.Host
나는 이것이 오래되었다는 것을 알고 있지만 지금 이것을하는 올바른 방법은
string Domain = HttpContext.Current.Request.Url.Authority
서버용 포트가있는 DNS 또는 IP 주소를 가져옵니다.
Match match = Regex.Match(host, "([^.]+\\.[^.]{1,3}(\\.[^.]{1,3})?)$");
string domain = match.Groups[1].Success ? match.Groups[1].Value : null;
host.com => return host.com
s.host.com => return host.com
host.co.uk => return host.co.uk
www.host.co.uk => return host.co.uk
s1.www.host.co.uk => return host.co.uk
This works also:
string url = HttpContext.Request.Url.Authority;
C# Example Below:
string scheme = "http://";
string rootUrl = default(string);
if (Request.ServerVariables["HTTPS"].ToString().ToLower() == "on")
{
scheme = "https://";
}
rootUrl = scheme + Request.ServerVariables["SERVER_NAME"].ToString();
string host = Request.Url.Host;
Regex domainReg = new Regex("([^.]+\\.[^.]+)$");
HttpCookie cookie = new HttpCookie(cookieName, "true");
if (domainReg.IsMatch(host))
{
cookieDomain = domainReg.Match(host).Groups[1].Value;
}
This will return specifically what you are asking.
Dim mySiteUrl = Request.Url.Host.ToString()
I know this is an older question. But I needed the same simple answer and this returns exactly what is asked (without the http://).
참고URL : https://stackoverflow.com/questions/1214607/how-can-i-get-the-root-domain-uri-in-asp-net
'program story' 카테고리의 다른 글
Vim : 두 번째 행마다 삭제하는 방법? (0) | 2020.09.04 |
---|---|
Linux / UNIX에서 현재 네트워크 인터페이스 처리량 통계를 얻으려면 어떻게해야합니까? (0) | 2020.09.04 |
Git에 동시에 여러 파일을 추가하는 방법 (0) | 2020.09.04 |
AngularJS 형식 JSON 문자열 출력 (0) | 2020.09.04 |
카운터에 변수 "i"및 "j"가 사용되는 이유는 무엇입니까? (0) | 2020.09.04 |