2009-03-27 15 views
15

Tek bir aramada HttpContext.Current.Request.Url.Host ve HttpContext.Current.Request.ApplicationPath almak için herhangi bir yolu var mı?Request.Url.Host ve ApplicationPath

"Tam uygulama URL'si" gibi bir şey?

DÜZENLEME: -:

http://[www.mysite.com/mywebapp]/Pages/Default.aspx 

Sadece meraktan sormak Açıklama ihtiyacım olanı [] içinde parçasıdır.

DÜZEN 2: Tüm yanıtlar için teşekkürler, ancak bunların hiçbiri tam olarak aradığım şey değildi. Bilginize, ben bu şekilde sorunu çözmüş (ama daha pürüzsüz bir yol varsa bilerek hala ilgileniyorum):

public string GetWebAppRoot() 
{ 
    if(HttpContext.Current.Request.ApplicationPath == "/") 
     return "http://" + HttpContext.Current.Request.Url.Host; 
    else 
     return "http://" + HttpContext.Current.Request.Url.Host + HttpContext.Current.Request.ApplicationPath; 
} 
+0

Request.Url.Authority'yi teoride istersiniz; Ana bilgisayarın bağlantı noktası yok. Ne yazık ki, en iyi bahsiniz, istemcinin bağlandığı sunucunun dns: portunu içermesi gerektiği için Request.Headers ["HOST"] öğesinde bulunanları kullanmaktır. HTTP/1.0 isteklerinden endişe ediyorsanız, elbette Request.Url.Host'a geri dönebilirsiniz. İstemcinin bağlandığı "HOST" başlığındaki bağlantı noktası değil, sunucu yazılımının bağlı olduğu bağlantı noktasına dönmek için Request.Url.Authority'yi şahsen deneyimledim. (FWIW, SSRS 2012'nin Rapor Yöneticisi web sitesinde idi.) – Granger

cevap

29
public static string GetSiteRoot() 
{ 
    string port = System.Web.HttpContext.Current.Request.ServerVariables["SERVER_PORT"]; 
    if (port == null || port == "80" || port == "443") 
    port = ""; 
    else 
    port = ":" + port; 

    string protocol = System.Web.HttpContext.Current.Request.ServerVariables["SERVER_PORT_SECURE"]; 
    if (protocol == null || protocol == "0") 
    protocol = "http://"; 
    else 
    protocol = "https://"; 

    string sOut = protocol + System.Web.HttpContext.Current.Request.ServerVariables["SERVER_NAME"] + port + System.Web.HttpContext.Current.Request.ApplicationPath; 

    if (sOut.EndsWith("/")) 
    { 
    sOut = sOut.Substring(0, sOut.Length - 1); 
    } 

    return sOut; 
} 
+0

IIRC SERVER_NAME size ihtiyacınız olan ana bilgisayar adını alamayabilir - ör. ana bilgisayar başlığı çözünürlüğü (http://msdn.microsoft.com/en-us/library/ms524602.aspx)! Eğer yanılıyorsam beni düzeltin :) – veggerby

+2

Yıllarca, farklı sitelerde kullandım - ve size, http: // 'dan başlayarak http: //' den ilk/ – MartinHN

+0

olan isteklerin sunucu adını verir. eserler, bir demet teşekkürler. Standart olmayan bağlantı noktaları ve https://www.blah.com/web/test/hello.aspx gibi URL'lerle çalışacak bir şey arıyordum. Ben bir spin vereceğim. – infocyde

2

Kontrol this post:

public static Uri GetBaseUrl(HttpRequest request) 
{ 
    Uri contextUri = new Uri(request.Url, request.RawUrl); 
    UriBuilder realmUri = new UriBuilder(contextUri) { Path = request.ApplicationPath, Query = null, Fragment = null }; 
    return realmUri.Uri; 
} 

public static string GetAbsoluteUrl(HttpRequest request, string relativeUrl) 
{ 
    return new Uri(GetBaseUrl(request), VirtualPathUtility.ToAbsolute(relativeUrl)).AbsoluteUri; 
} 

size ne gerek alamazsanız direcly, yapmanın mümkün olmalıdır GetBaseUrl:

GetAbsoluteUrl(HttpContext.Current.Request, "/")

1
HttpContext.Current.Request.Url.AbsoluteUri 
+0

Teşekkürler, ama hayır - bu size aradığı ASPX sayfasının tam yolunu veriyor. Sadece ana makine + uygulama yolu istiyorum, sayfa yolunu değil. –

3

Tüm cevaplar için teşekkürler, ancak bunların hiçbiri tam olarak aradığım şey değildi. Bilginize, ben bu şekilde sorunu çözmüş (ama daha pürüzsüz bir yol varsa bilerek hala ilgileniyorum):

public string GetWebAppRoot() 
{ 
    if(HttpContext.Current.Request.ApplicationPath == "/") 
     return "http://" + HttpContext.Current.Request.Url.Host; 
    else 
     return "http://" + HttpContext.Current.Request.Url.Host + HttpContext.Current.Request.ApplicationPath; 
} 
+0

https ise? –

+0

Evet, çözümüm bunu çözmüyor, ancak bu: dönmek String.Format ("{0}: // {1} /", Request.Url.Scheme, Request.Url.Host); –

5

Ne gerçekten yapmalıyım:

return String.Format("{0}://{1}/", Request.Url.Scheme, Request.Url.Host); 

Bu şekilde sizin çalışır HTTPS kullanan

Bu bir port numarası benim localhost üzerinde çalışıyor değildi
16

öylesine küçük bir modifikasyonu yapılan (veya başka bir şema!):

private string GetWebAppRoot() 
    { 
     string host = (HttpContext.Current.Request.Url.IsDefaultPort) ? 
      HttpContext.Current.Request.Url.Host : 
      HttpContext.Current.Request.Url.Authority; 
     host = String.Format("{0}://{1}", HttpContext.Current.Request.Url.Scheme, host);    
     if (HttpContext.Current.Request.ApplicationPath == "/") 
      return host; 
     else 
      return host + HttpContext.Current.Request.ApplicationPath; 
    } 
İlgili konular