建议写在Global.asax 的函数Session_Start内
void Session_Start(object sender, EventArgs e) { // 在新会话启动时运行的代码 string Path = string.Empty; string pre = Request.ServerVariables["http_host"].ToString().Trim(); string pos = Request.ServerVariables["path_info"].ToString().Trim(); int i; i = pre.IndexOf("localhost:"); if (i < 0) { Path = pre + "/"; } else { Path = pre + pos.Substring(0, pos.LastIndexOf("/") + 1); } Session["Path"] = "http://" + Path; }