मान लीजिए कि मैं एक वेबसाइट http://www.foobar.com पर होस्ट कर रहा हूं ।
क्या कोई ऐसा तरीका है जिससे मैं प्रोग्रामिक रूप से " http://www.foobar.com/ " को अपने कोड में (यानी अपने वेब कॉन्फिग में इसे हार्डकोड किए बिना) पता लगा सकता हूं ?
मान लीजिए कि मैं एक वेबसाइट http://www.foobar.com पर होस्ट कर रहा हूं ।
क्या कोई ऐसा तरीका है जिससे मैं प्रोग्रामिक रूप से " http://www.foobar.com/ " को अपने कोड में (यानी अपने वेब कॉन्फिग में इसे हार्डकोड किए बिना) पता लगा सकता हूं ?
जवाबों:
HttpContext.Current.Request.Url आप URL पर सभी जानकारी प्राप्त कर सकते हैं। और इसके टुकड़ों में यूआरएल को तोड़ सकता है।
string baseUrl = Request.Url.GetLeftPart(UriPartial.Authority);
GetLeftPart पद्धति एक स्ट्रिंग देता है जिसमें 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;
}
}
context.Request.Url.Port == 80 द्वारा (context.Request.Url.Port == 80 && context.Request.Url.Scheme == "http") || (context.Request.Url.Port == 443 && context.Request.Url.Scheme == "https")या उपयोग जवाब नीचे
यदि उदाहरण उरल 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"
.Hostका "http://www.foobar.com/Page1"है www.foobar.com, नहीं foobar.com।
संपूर्ण अनुरोध URL स्ट्रिंग प्राप्त करने के लिए:
HttpContext.Current.Request.Url
अनुरोध के www.foo.com भाग पाने के लिए:
HttpContext.Current.Request.Url.Host
ध्यान दें कि आप कुछ हद तक अपने ASP.NET अनुप्रयोग के बाहर कारकों की दया पर हैं। यदि IIS आपके एप्लिकेशन के लिए कई या किसी भी होस्ट हेडर को स्वीकार करने के लिए कॉन्फ़िगर किया गया है, तो उन डोमेन में से कोई भी जो DNS के माध्यम से आपके आवेदन का समाधान करता है, अनुरोध यूआरएल के रूप में प्रदर्शित हो सकता है, जिसके आधार पर उपयोगकर्ता ने प्रवेश किया था।
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 => वापसी host.com
host.co.uk => वापसी host.co.uk
www.host.co.uk => वापसी host.co.uk
s1.www.host.co.uk => वापसी host.co.uk
मुझे पता है कि यह पुराना है लेकिन अब ऐसा करने का सही तरीका है
string Domain = HttpContext.Current.Request.Url.Authority
सर्वर के लिए पोर्ट के साथ DNS या आईपी एड्रेस मिलेगा।
यह भी काम करता है:
string url = HttpContext.Request.Url.Authority;
नीचे # उदाहरण
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;
}
Requestवस्तु में देखने का प्रयास कर सकते हैं ।