source

ASP에서 루트 도메인 URI를 가져오려면 어떻게 해야 합니까?NET?

ittop 2023. 4. 27. 22:44
반응형

ASP에서 루트 도메인 URI를 가져오려면 어떻게 해야 합니까?NET?

제가 http://www.foobar.com 에서 웹사이트를 호스팅하고 있다고 가정해 보겠습니다.

뒤에 있는 코드에서 "http://www.foobar.com/ "을 프로그래밍 방식으로 확인할 수 있는 방법이 있습니까(즉, 웹 구성에서 하드 코딩할 필요가 없습니다)?

string baseUrl = Request.Url.GetLeftPart(UriPartial.Authority);

URI::GetLeftPart 메서드:

GetLeftPart 메서드는 URI 문자열의 가장 왼쪽 부분을 포함하는 문자열을 반환하며 부분으로 지정된 부분으로 끝납니다.

URI 부분 열거:

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;
    }
}

HttpContext입니다.현재.요청합니다.URL은 URL의 모든 정보를 제공하고 URL을 조각으로 나눌 수 있습니다.

예를 들어 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"
string hostUrl = Request.Url.Scheme + "://" + Request.Url.Host; //should be "http://hostnamehere.com"

전체 요청 URL 문자열을 가져오는 방법

HttpContext.Current.Request.Url

요청의 www.foo.com 부분을 가져오는 방법

HttpContext.Current.Request.Url.Host

당신은 어느 정도 ASP 외부 요인에 좌우된다는 점에 유의하십시오.NET 응용 프로그램.IIS가 응용프로그램에 대해 여러 개의 호스트 헤더 또는 호스트 헤더를 허용하도록 구성된 경우, 사용자가 입력한 도메인에 따라 DNS를 통해 응용프로그램에 확인된 도메인이 요청 URL로 나타날 수 있습니다.

Match match = Regex.Match(host, "([^.]+\\.[^.]{1,3}(\\.[^.]{1,3})?)$");
string domain = match.Groups[1].Success ? match.Groups[1].Value : null;

host.com => 반품 host.com
s.host.com => 반품 host.com

host.co.uk => 반품 host.co.uk
www.host.co.uk => 반품 host.co.uk
s1.www.host.co.uk => 반품 host.co.uk

--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 주소를 가져옵니다.

이 기능은 다음과 같습니다.

string url = HttpContext.부탁한다.Url.권한;

C# 아래의 예:

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;                                
}

그러면 구체적으로 요청한 내용이 반환됩니다.

Dim mySiteUrl = Request.Url.Host.ToString()

이것이 오래된 질문이라는 것을 압니다.하지만 저는 동일한 간단한 답변이 필요했고 이것은 (http:// 없이) 정확히 질문된 내용을 반환합니다.

언급URL : https://stackoverflow.com/questions/1214607/how-can-i-get-the-root-domain-uri-in-asp-net

반응형