





















public static string GetRootURI()
{
string AppPath = "";
HttpContext HttpCurrent = HttpContext.Current;
HttpRequest Req;
if (HttpCurrent != null)
{
Req = HttpCurrent.Request;string UrlAuthority = Req.Url.GetLeftPart(UriPartial.Authority);
if (Req.ApplicationPath == null || Req.ApplicationPath == "/")
//直接安装在 Web 站点
AppPath = UrlAuthority;
else
//安装在虚拟子目录下
AppPath = UrlAuthority + Req.ApplicationPath;
}
return AppPath;
}
在ASP.NET中可以对Reqeust对象中获取web应用程序或网站的虚拟根目录,物理文件路径等信息,可以解析出URI的相关目录.1:测试代码
测试URL :http:
//localhost:8080/Default2.aspxRequest.RawUrl: 获取客户端请求的 URL 信息(不包括主机和端口)
------> /Default2.aspx查下使用Server.MapPath进行虚拟目录映射的问题,我进行了一把试验:
试验条件:w1网站物理路径为F:\temp\代码示例\WebSite1,默认网站的物理路径C:\inetpub\wwwroot,对于w1网站的某网页调用 Server.MapPath方法。
试验结果:
1 Server.MapPath("") w1返回F:\temp\代码示例\WebSite1 ,即w1网站物理路径2 Server.MapPath("/") w1返回C:\inetpub\wwwroot,即默认网站物理路径3 Server.MapPath("Bin") w1返回F:\temp\代码示例\WebSite1\Bin
4 Server.MapPath("/Bin") w1返回C:\inetpub\wwwroot\Bin返回的是默认网站物理路径+/Bin。
总结下:参数是以斜杠开头( / 或 \ )或者../ 或 ..\ 等开头,那么首先取默认网站的物理路径,即C:\Inetpub\wwwroot,再加上传入的传入的路径参数;如果不是以这类符号开头的话,就是取当前网站的物理路径,即F:\temp\代码示例\WebSite1 ,再加上传入的路径参数。
注意:Server.MapPath不保证返回的物理路径是有效路径。
试验了几把发现都还正确,不知道这种考虑方式是否正确,
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。