随笔-7  评论-24  文章-102  trackbacks-0


引用自:
asp.net获取网站路径
http://hi.baidu.com/zhangfengbang/blog/item/5f99d74b0ce661f883025cbc.html
HttpRequest 成员
http://msdn.microsoft.com/zh-cn/library/system.web.httprequest_members(v=VS.80).aspx
ASP.NET获取网站根目录的url的函数,很简单
http://www.xueit.com/html/2009-03/21_932_00.html
 



网站在服务器磁盘上的物理路径: HttpRuntime.AppDomainAppPath
虚拟程序路径: HttpRuntime.AppDomainAppVirtualPath


任何于Request/HttpContext.Current等相关的方法, 都只能在有请求上下文或者页面时使用. 即在无请求上下文时,HttpContext.Current为null. 而上面提到的方法一直可用.

对于全局Cache对象的访问亦然.



示例:输出asp.net 网站路径。 

private void responseHtml()
    {
         System.Text.StringBuilder sb 
= new System.Text.StringBuilder();
  
//输出:当前时间: 2007-08-30 11:03:49
         sb.Append(string.Format("当前时间: {0}", Server.HtmlEncode(DateTime.Now.ToString())));
         sb.Append(
"<br />");

  
//当前请求的虚拟路径: /aDirectory/Sample/responseHtml.aspx
         sb.Append(string.Format("当前请求的虚拟路径: {0}",Server.HtmlEncode(Request.CurrentExecutionFilePath)));
         sb.Append(
"<br />");

  
//获取当前应用程序的根目录路径: /aDirectory
         sb.Append(string.Format("获取当前应用程序的根目录路径: {0}", Server.HtmlEncode(Request.ApplicationPath)));
         sb.Append(
"<br />");

  
//当前请求的虚拟路径: /aDirectory/Sample/responseHtml.aspx
         sb.Append(string.Format("当前请求的虚拟路径: {0}",Server.HtmlEncode(Request.FilePath)));
         sb.Append(
"<br />");

  
//当前请求的虚拟路径: /aDirectory/Sample/responseHtml.aspx
         sb.Append(string.Format("当前请求的虚拟路径: {0}",Server.HtmlEncode(Request.Path)));
         sb.Append(
"<br />");

  
//获取当前正在执行的应用程序的根目录的物理文件系统路径: E:\Visual Studio 2005\
         sb.Append(string.Format("获取当前正在执行的应用程序的根目录的物理文件系统路径: {0}", Server.HtmlEncode(Request.PhysicalApplicationPath)));
         sb.Append(
"<br />");

  
//获取与请求的 URL 相对应的物理文件系统路径: E:\Visual Studio 2005\\aDirectory\
         sb.Append(string.Format("获取与请求的 URL 相对应的物理文件系统路径: {0}", Server.HtmlEncode(Request.PhysicalApplicationPath)));
         sb.Append(
"<br />");
         Response.Write(sb.ToString());
     }

 



在ASP.NET编程中经常需要用Request获取url的有关信息.

测试的url地址是http://www.test.com/testweb/default.aspx, 结果如下:
Request.ApplicationPath:                     /testweb
Request.CurrentExecutionFilePath:       /testweb/default.aspx
Request.FilePath:                                /testweb/default.aspx
Request.Path:                                     /testweb/default.aspx
Request.PathInfo:
Request.PhysicalApplicationPath:          E:\WWW\testweb\
Request.PhysicalPath:                         E:\WWW\testweb\default.aspx
Request.RawUrl:                                 /testweb/default.aspx
Request.Url.AbsolutePath:                    /testweb/default.aspx
Request.Url.AbsoluteUri:                      http://www.test.com/testweb/default.aspx
Request.Url.Host:                                www.test.com
Request.Url.LocalPath:                        /testweb/default.aspx 

当url中带参数时可以使用:
HttpContext.Current.Request.Url.PathAndQuery.ToString()//

本页地址:   Request.URL; 

上页地址:  
Request.UrlReferrer  
Request.ServerViables["http_referer"]  
Request.RawUrl  
Request.RawUrl.QueryAndPath  
System.IO.Path.GetFileName(Request.FilePath.ToString()) 




获取网站根目录的url源代码--2010.05.09
http://www.xueit.com/html/2009-03/21_932_00.html

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;
    }

可修改为静态属性 

private
 static string rootURI;

    
/// <summary>
    
/// 获得网站根目录的url的函数
    
/// </summary>
    
/// <returns>应用程序根目录 eg:  http://www.xxx.net:2156/im </returns>
    public static string RootURI
    {
        
get
        {
            
if (string.IsNullOrEmpty(rootURI))
            {
                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   站点   
                        rootURI = UrlAuthority;
                    
else
                        
//安装在虚拟子目录下   
                        rootURI = UrlAuthority + Req.ApplicationPath;
                }
            }

            
return rootURI;
        }
    }

posted on 2010-05-09 00:02 黄小二 阅读(736) 评论(0)  编辑  收藏 所属分类: ASP.NET

只有注册用户登录后才能发表评论。


网站导航: