2010-08-11 4 views
0

Je l'utilise pour obtenir la racine de l'application et je me demandais si c'est une meilleure façon de le faire?Comment obtenir la racine de l'application Web?

string root = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + HttpContext.Current.Request.ApplicationPath.TrimEnd('/'); 

Répondre

3

Cela peut sembler aller trop loin mais c'est la routine que j'utilise. Je n'ai pas encore eu de problèmes avec ça.

public class UrlHelper 
{ 
    public static string ApplicationBaseUrl() 
    { 
     string port = HttpContext.Current.Request.ServerVariables["SERVER_PORT"]; 

     if (port == null || port == "80" || port == "443") 
      port = ""; 
     else 
      port = ":" + port; 

     string protocol = HttpContext.Current.Request.ServerVariables["SERVER_PORT_SECURE"]; 

     if (protocol == null || protocol == "0") 
      protocol = "http://"; 
     else 
      protocol = "https://"; 

     return protocol + HttpContext.Current.Request.ServerVariables["SERVER_NAME"] + port + 
       HttpContext.Current.Request.ApplicationPath; 
    } 
} 
+0

Merci, je vais essayer. – VoodooChild

Questions connexes