我正在使用它来获取应用程序root并且想知道这是否是更新的更好的方法呢?
string root = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + HttpContext.Current.Request.ApplicationPath.TrimEnd('/');
答案 0 :(得分:3)
它可能似乎过分但这是我使用的帮助程序。我还没有遇到任何麻烦。
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;
}
}