一、AppDomain
1.AppDomin獲取當前前程序域目錄
2.不需要請求上線文實例,例如在Global.ascx中訪問等
//網站物理目錄 AppDomain.CurrentDomain.BaseDirectory //網站動態目錄 AppDomain.CurrentDomain.DynamicDirectory
結果:
網站物理目錄: E:\自開發控件\Pager\PageBar\
網站動態目錄: C:\Users\tianma\AppData\Local\Temp\Temporary ASP.NET Files\vs\7d482ba1\5e597c25
二、Request.Mapth() / Server.Mappath()
比較常用的是使用MapPath() 方法
1.Server.Mapth() 示例
<p> 網站物理目錄: <%=Server.MapPath("~") %> </p> <p> 請求文件夾目錄: <%=Server.MapPath(".") %> </p> <p> 網站物理目錄: <%=Server.MapPath("..") %> </p> <p> 文件目錄: <%=Server.MapPath("~/view/view1.html") %> </p>
結果:
網站物理目錄: E:\自開發控件\Pager\PageBar\
請求文件夾目錄: E:\自開發控件\Pager\PageBar\View
網站物理目錄: E:\自開發控件\Pager\PageBar
文件目錄: E:\自開發控件\Pager\PageBar\view\view1.html
2.Request.Mapth() 使用方式同上,但是Request有更多擴展方便路徑訪問
<p> 當前網站物理路徑: <%=Request.PhysicalApplicationPath %> </p> <p> 當前請求文件物理路徑: <%=Request.PhysicalPath %> </p>
結果:
當前網站物理路徑: E:\自開發控件\Pager\PageBar\
當前請求文件物理路徑: E:\自開發控件\Pager\PageBar\View\directory1.aspx
3.Request 訪問獲取虛擬路徑和URL信息
// 獲取服務器上 ASP.NET 應用程序的虛擬應用程序根路徑:
Request.ApplicationPath; // /aspnet // 獲取應用程序根的虛擬路徑,並通過對應用程序根使用波形符 (~) 表示法使該路徑成為相對路徑。 Request.AppRelativeCurrentExecutionFilePath; // ~/zz/zz.aspx // 獲取當前請求的虛擬路徑 Request.CurrentExecutionFilePath; // /aspnet/zz/zz.aspx Request.FilePath; // /aspnet/zz/zz.aspx // 獲取CurrentExecutionFilePath屬性中指定的文件名的擴展名。 Request.CurrentExecutionFilePathExtension; // .aspx // 獲取當前請求的虛擬路徑(包括附件路徑信息) Request.Path; // /aspnet/zz/zz.aspx/info // 獲取具有 URL 擴展名的資源的附加路徑信息。 Request.PathInfo; // /info // 獲取有關當前請求的 URL 的信息。 Request.Url; // http://localhost/aspnet/zz/zz.aspx/inf?name=wk // 獲取當前請求的原始 URL Request.RawUrl; // /aspnet/zz/zz.aspx/inf?name=wk // 獲取有關客戶端上次請求的 URL 的信息,該請求鏈接到當前的 URL。 Request.UrlReferrer; // System.Uri
更多: