隨筆-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 黃小二 閱讀(756) 評論(0)  編輯  收藏 所屬分類: ASP.NET
          主站蜘蛛池模板: 衡东县| 衡阳市| 青铜峡市| 泗水县| 金溪县| 滦南县| 曲靖市| 唐山市| 淮北市| 光泽县| 大化| 汽车| 繁峙县| 调兵山市| 开化县| 新昌县| 丰城市| 周宁县| 湖北省| 将乐县| 临西县| 剑阁县| 云梦县| 南丰县| 乌拉特中旗| 云和县| 孝义市| 三穗县| 樟树市| 科尔| 新乡县| 张家口市| 克东县| 手游| 剑河县| 宁陕县| 湖口县| 常德市| 彭州市| 九寨沟县| 乐都县|