Advertisement
scottgal

Get Site Root

Sep 25th, 2015
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.95 KB | None | 0 0
  1.  public static string GetSiteRoot()
  2.         {
  3.             if (HttpContext.Current == null) throw new NullReferenceException("Context is not available");
  4.             var port = HttpContext.Current.Request.ServerVariables["SERVER_PORT"];
  5.             if (port == null || port == "80" || port == "443")
  6.                 port = "";
  7.             else
  8.                 port = ":" + port;
  9.  
  10.             var protocol = HttpContext.Current.Request.ServerVariables["SERVER_PORT_SECURE"];
  11.             if (protocol == null || protocol == "0")
  12.                 protocol = "http://";
  13.             else
  14.                 protocol = "https://";
  15.  
  16.             var sOut = protocol + HttpContext.Current.Request.ServerVariables["SERVER_NAME"] + port +
  17.                        HttpContext.Current.Request.ApplicationPath;
  18.  
  19.             if (sOut.EndsWith("/"))
  20.             {
  21.                 sOut = sOut.Substring(0, sOut.Length - 1);
  22.             }
  23.  
  24.             return sOut;
  25.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement