2009-03-27 15 views
15

क्या किसी कॉल में HttpContext.Current.Request.Url.Host और HttpContext.Current.Request.ApplicationPath प्राप्त करने का कोई तरीका है?Request.Url.Host और ApplicationPath एक कॉल में

कुछ "पूर्ण एप्लिकेशन यूआरएल" की तरह कुछ?

संपादित करें: -:

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

मैं बस जिज्ञासा से बाहर पूछना स्पष्टीकरण मैं क्या जरूरत है [] के भीतर इस हिस्सा है।

संपादित करें 2: सभी उत्तरों के लिए धन्यवाद, लेकिन उनमें से कोई भी बिल्कुल वैसा ही नहीं था जिसे मैं ढूंढ रहा था। FYI करें, मैं इस समस्या इस तरह से हल (लेकिन अभी भी यह जानकर वहाँ एक चिकनी तरीका है में दिलचस्पी है):

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, सिद्धांत में; होस्ट में बंदरगाह नहीं है। दुर्भाग्यवश, आपका सर्वोत्तम शर्त अनुरोध है कि अनुरोध में है। हेडर ["HOST"], क्योंकि इसमें डीएनएस शामिल है: सर्वर का बंदरगाह जो क्लाइंट कनेक्ट करने के लिए उपयोग किया जाता है। यदि आप HTTP/1.0 अनुरोधों के बारे में चिंतित हैं, तो आप निश्चित रूप से Request.Url.Host पर वापस आ सकते हैं। मैंने व्यक्तिगत रूप से Request.Url.Arl. प्राधिकरण का अनुभव किया है जो सर्वर सॉफ़्टवेयर बाध्य है, "HOST" शीर्षलेख में बंदरगाह नहीं है --- जो क्लाइंट कनेक्ट करने के लिए उपयोग किया जाता है। (एफडब्ल्यूआईडब्ल्यू, यह एसएसआरएस 2012 की रिपोर्ट मैनेजर वेबसाइट में था।) – Granger

उत्तर

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

आईआईआरसी SERVER_NAME आपको होस्ट होस्ट नहीं मिल सकता है - उदा। होस्ट-हेडर रिज़ॉल्यूशन (http://msdn.microsoft.com/en-us/library/ms524602.aspx)! अगर मैं गलत हूं तो मुझे सही करें :) – veggerby

+2

मैंने इसे विभिन्न साइटों पर वर्षों से उपयोग किया है - और यह आपको अनुरोध का सर्वर नाम देता है, जो http: // से पहले/ – MartinHN

+0

पर है यदि यह काम करता है, एक गुच्छा धन्यवाद। ऐसा कुछ ढूंढ रहा था जो गैर मानक बंदरगाहों और https://www.blah.com/web/test/hello.aspx जैसे यूआरएल के साथ काम करेगा। मैं इसे एक स्पिन दे दूंगा। – infocyde

2

चेक 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; 
} 

आप क्या से की जरूरत नहीं मिलता है direcly, है क्या करने के लिए संभव हो जाना चाहिए GetBaseUrl:

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

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

धन्यवाद, लेकिन नहीं - यह आपको एएसपीएक्स-पेज का पूरा मार्ग देता है जिसे इसे से बुलाया जाता है। मैं केवल मेजबान + अनुप्रयोग पथ चाहता हूं, पृष्ठ पथ नहीं। –

3

सभी उत्तरों के लिए धन्यवाद, लेकिन उनमें से कोई भी बिल्कुल वैसा ही नहीं था जिसे मैं ढूंढ रहा था। FYI करें, मैं इस समस्या इस तरह से हल (लेकिन अभी भी यह जानकर हो, तो एक चिकनी रास्ते में दिलचस्पी है):

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 है? –

+0

हां, मेरा समाधान वास्तव में इसे संभाल नहीं करता है, लेकिन यह होगा: वापसी स्ट्रिंग। फोर्मेट ("{0}: // {1} /", Request.Url.Scheme, Request.Url.Host); –

5

क्या तुम सच में क्या करना चाहिए है:

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

यह इसी तरह से अगर आप काम करता है HTTPS का उपयोग कर रहे हैं

16

यह एक पोर्ट संख्या के साथ अपने स्थानीय होस्ट पर काम नहीं कर रहा था तो मामूली संशोधन किए गए (या कुछ अन्य स्कीमा!):

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