使用C#来实现:
public static string GetRootPath() { // 是否为SSL认证站点 string secure = HttpContext.Current.Request.ServerVariables["HTTPS"]; string httpProtocol = (secure == "on" ? "https://" : "http://"); // 服务器名称 string serverName = HttpContext.Current.Request.ServerVariables["Server_Name"]; string port = HttpContext.Current.Request.ServerVariables["SERVER_PORT"]; // 应用服务名称 string applicationName = HttpContext.Current.Request.ApplicationPath; return httpProtocol + serverName + (port.Length > 0 ? ":" + port : string.Empty) + applicationName; }
使用js来实现:
//js获取项目根路径http://localhost:1396/Web function getRootPath() { return window.location.protocol+"//"+window.location.host; }