获取网站根目录的urli源代码

 

     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.aspx

      Response.Write(
" Request.RawUrl: "   +   "  获取客户端请求的 URL 信息(不包括主机和端口)------>  "   +  Request.RawUrl  +   " <br /> " );
        Response.Write(
" Request.ApplicationPath : "   +   " 获取服务器上 ASP.NET 应用程序的虚拟路径。------>  "   +  Request.ApplicationPath  +   " <br /> " );
        Response.Write(
" Request.CurrentExecutionFilePath : "   +   "  获取当前请求的虚拟路径。 ------>  "   +  Request.CurrentExecutionFilePath  +   " <br /> " );
        Response.Write(
" Request.Path : "   +   "  获取当前请求的虚拟路径。 ------>  "   +  Request.Path  +   " <br /> " );
        Response.Write(
" Request.PathInfo :  "   +   "  取具有 URL 扩展名的资源的附加路径信息 ------> "   +  Request.PathInfo  +   " <br /> " );
        Response.Write(
" Request.PhysicalPath:  "   +   "  获取与请求的 URL 相对应的物理文件系统路径。 ------> "   +  Request.PhysicalPath  +   " <br /> " );
        Response.Write(
" Request.Url.LocalPath :  "   +   "     ------> "   +  Request.Url.LocalPath  +   " <br /> " );
        Response.Write(
" Request.Url.AbsoluteUri:  "   +   "  ------> "   +  Request.Url.AbsoluteUri  +   " <br /> " );
        Response.Write(
" Request.Url.AbsolutePath :  "   +   "  ---------------------- ------> "   +  Request.Url.AbsolutePath  +   " <br /> " );

2 :测试结果

Request.RawUrl: 获取客户端请求的 URL 信息(不包括主机和端口)
------>   / Default2.aspx
Request.ApplicationPath :获取服务器上 ASP.NET 应用程序的虚拟路径。
------>   /
Request.CurrentExecutionFilePath : 获取当前请求的虚拟路径。 
------>   / Default2.aspx
Request.Path : 获取当前请求的虚拟路径。 
------>   / Default2.aspx
Request.PathInfo : 取具有 URL 扩展名的资源的附加路径信息 
------>
Request.PhysicalPath: 获取与请求的 URL 相对应的物理文件系统路径。 
------> E:\temp\Default2.aspx
Request.Url.LocalPath : 
------>/ Default2.aspx
Request.Url.AbsoluteUri: 
------> http: // localhost:8080/Default2.aspx
Request.Url.AbsolutePath :  ----------------------   ------>/ Default2.aspx

 


 查下使用Server.MapPath进行虚拟目录映射的问题,我进行了一把试验:

试验条件:w1网站物理路径为F:\temp\代码示例\WebSite1,默认网站的物理路径C:\inetpub\wwwroot,对于w1网站的某网页调用 Server.MapPath方法。

 试验结果:

  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不保证返回的物理路径是有效路径。

 
试验了几把发现都还正确,不知道这种考虑方式是否正确,

 

 

你可能感兴趣的:(获取网站根目录的urli源代码)