刚才看到的 Dim UrlAuthority As String = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) If HttpContext.Current.Request.ApplicationPath = "/" Then ' 直接安装在 Web 站点 HttpContext.Current.Session("AppPath") = UrlAuthority Else ' 安装在虚拟子目录下 HttpContext.Current.Session("AppPath") = UrlAuthority & HttpContext.Current.Request.ApplicationPath End If
Dim UrlAuthority As String =
HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority)
If HttpContext.Current.Request.ApplicationPath = "/" Then
' 直接安装在 Web 站点
HttpContext.Current.Session("AppPath") = UrlAuthority
Else
' 安装在虚拟子目录下
HttpContext.Current.Session("AppPath") =
UrlAuthority & HttpContext.Current.Request.ApplicationPath
End If
###########################我#$%^$^#@&#%%*)(^)#^$@#
如果是写在后台代码里面,使用"~/"
在asp.net后台代码中,用 "~/"
{
//取URL虚拟路径
string VirtualSrc = System.Web.HttpContext.Current.Request.Url.ToString();
int NameCut = VirtualSrc.LastIndexOf("/");
//保存参数
string SavePath = Path;
string TreePath = System.Web.HttpContext.Current.Request.MapPath(@"~/");
string UpDirectory = "";
//截取文件名
VirtualSrc = VirtualSrc.Remove(NameCut + 1);
//如果HTTP带参数则保留参数
if (Path.LastIndexOf(@"?") >= 0)
{
Path = Path.Remove(Path.LastIndexOf(@"?"));
//取得上一级与上n级目录
UpDirectory = System.Web.HttpContext.Current.Request.MapPath(Path).Replace(TreePath, "").Replace(@"\", "/").Replace(Path, "");
}
else
{
//取得上一级与上n级目录
UpDirectory = System.Web.HttpContext.Current.Request.MapPath(Path).Replace(TreePath, "").Replace(@"\", "/").Replace(Path, ""); }
if (UpDirectory != "")
{
VirtualSrc = VirtualSrc.Replace(UpDirectory, "");
}
return VirtualSrc + SavePath;
}这是我最近一个项目解决的方法:如你要到根目录下的某个文件可以这样调用:pathtov("js/xx.js")
你网站的网址是:httP://www.xxx.com这个方法就会返回
httP://www.xxx.com/js/xx.js,你现在在网站任何地方调都不会有路径问题了