Diciamo che sto ospitando un sito web su http://www.foobar.com .
C'è un modo in cui posso accertare programmaticamente " http://www.foobar.com/ " nel mio codice sottostante (cioè senza doverlo codificare nella mia configurazione web)?
Diciamo che sto ospitando un sito web su http://www.foobar.com .
C'è un modo in cui posso accertare programmaticamente " http://www.foobar.com/ " nel mio codice sottostante (cioè senza doverlo codificare nella mia configurazione web)?
Risposte:
HttpContext.Current.Request.Url può ottenere tutte le informazioni sull'URL. E può scomporre l'URL nei suoi frammenti.
string baseUrl = Request.Url.GetLeftPart(UriPartial.Authority);
Il metodo GetLeftPart restituisce una stringa contenente la parte più a sinistra della stringa URI, che termina con la parte specificata da part.
Lo schema e i segmenti di autorità dell'URI.
Per chiunque ancora si chieda, una risposta più completa è disponibile su 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 da (context.Request.Url.Port == 80 && context.Request.Url.Scheme == "http") || (context.Request.Url.Port == 443 && context.Request.Url.Scheme == "https")o l'uso risposta qui sotto
Se l'URL di esempio è 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"
.Hostdi "http://www.foobar.com/Page1"è www.foobar.com, non foobar.com.
Per ottenere l'intera stringa dell'URL della richiesta:
HttpContext.Current.Request.Url
Per ottenere la parte www.foo.com della richiesta:
HttpContext.Current.Request.Url.Host
Nota che sei, in una certa misura, alla mercé di fattori esterni alla tua applicazione ASP.NET. Se IIS è configurato per accettare più o qualsiasi intestazione host per la tua applicazione, uno qualsiasi di quei domini che sono stati risolti nell'applicazione tramite DNS potrebbe essere visualizzato come URL di richiesta, a seconda di quello inserito dall'utente.
Match match = Regex.Match(host, "([^.]+\\.[^.]{1,3}(\\.[^.]{1,3})?)$");
string domain = match.Groups[1].Success ? match.Groups[1].Value : null;
host.com => restituisci host.com
s.host.com => restituisci host.com
host.co.uk => restituisci host.co.uk www.host.co.uk
=> restituisci host.co.uk
s1.www.host.co.uk => restituisci host.co.uk
So che è più vecchio ma il modo corretto per farlo ora è
string Domain = HttpContext.Current.Request.Url.Authority
Ciò otterrà il DNS o l'indirizzo IP con la porta per un server.
Funziona anche:
url stringa = HttpContext.Request.Url.Authority;
Esempio di C # di seguito:
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;
}
Requestnell'oggetto.