我想获取URL中除了当前页面以外的所有路径,例如:我的URL是http://www.MyIpAddress.com/red/green/default.aspx,我只想获取"http://www.MyIpAddress.com/red/green/"。我该如何获取?我正在尝试像这样做:
string sPath = new Uri(HttpContext.Current.Request.Url.AbsoluteUri).OriginalString; System.Web.HttpContext.Current.Request.Url.AbsolutePath;
sPath = sPath.Replace("http://", "");
System.IO.FileInfo oInfo = new System.IO.FileInfo(sPath);
string sRet = oInfo.Name;
Response.Write(sPath.Replace(sRet, ""));
在执行 new System.IO.FileInfo(sPath) 时,由于 sPath 包含 "localhost/red/green/default.aspx",导致出现异常,提示 "The given path's format is not supported."。
new Uri(myUrlString);
然后利用这些属性。 - AaronLS