SHOW:
|
|
- or go back to the newest paste.
1 | - | public static string GetSiteRoot() |
1 | + | public static string GetSiteRoot() |
2 | - | { |
2 | + | { |
3 | - | if (HttpContext.Current == null) throw new NullReferenceException("Context is not available"); |
3 | + | var port = HttpContext.Current.Request.Url.Authority; |
4 | - | var port = HttpContext.Current.Request.ServerVariables["SERVER_PORT"]; |
4 | + | var protocol = HttpContext.Current.Request.Url.Scheme; |
5 | - | if (port == null || port == "80" || port == "443") |
5 | + | var sOut = protocol + "://" + port + "/" + HttpContext.Current.Request.ApplicationPath; |
6 | - | port = ""; |
6 | + | |
7 | - | else |
7 | + | if (sOut.EndsWith("/")) |
8 | - | port = ":" + port; |
8 | + | sOut = sOut.Substring(0, sOut.Length - 1); |
9 | ||
10 | - | var protocol = HttpContext.Current.Request.ServerVariables["SERVER_PORT_SECURE"]; |
10 | + | var sOut = protocol + HttpContext.Current.Request.ServerVariables["SERVER_NAME"] + port + |
11 | - | if (protocol == null || protocol == "0") |
11 | + | HttpContext.Current.Request.ApplicationPath; |
12 | - | protocol = "http://"; |
12 | + | |
13 | - | else |
13 | + | if (sOut.EndsWith("/")) |
14 | - | protocol = "https://"; |
14 | + | { |
15 | sOut = sOut.Substring(0, sOut.Length - 1); | |
16 | - | var sOut = protocol + HttpContext.Current.Request.ServerVariables["SERVER_NAME"] + port + |
16 | + | } |
17 | - | HttpContext.Current.Request.ApplicationPath; |
17 | + | |
18 | return sOut; | |
19 | - | if (sOut.EndsWith("/")) |
19 | + | } |