ASP.NET根路徑的擷取和將Web網站下的絕對路徑轉換為虛擬路徑的兩種方案

來源:互聯網
上載者:User

ASP.NET 根路徑的擷取
        private string _ApplicationPath;  
        /// <summary>  
        /// 虛擬應用程式根路徑  
        /// </summary>  
        public string ApplicationPath  
        {  
            get 
            {  
                _ApplicationPath = HttpContext.Current.Request.ApplicationPath;  
                if (_ApplicationPath.Length == 1)  
                {  
                    _ApplicationPath = "";  
                }  
                return _ApplicationPath;  
            }  
        }  
        private string _CurrentPath;  
        /// <summary>  
        /// 當前的絕對路徑  
        /// </summary>  
        public string CurrentPath  
        {  
            get 
            {  
                _CurrentPath = HttpContext.Current.Server.MapPath(".").ToLower();//當前的絕對路徑(這裡MapPath裡的"."代表當前伺服器)  
                  
                if (_CurrentPath.Length == 1)  
                {  
                    _CurrentPath = "";  
                }  
                return _CurrentPath;  
            }  
        }  
        private string _RootPath;  
        /// <summary>  
        /// 系統的根目錄  
        /// </summary>  
        public string RootPath  
        {  
            get 
            {  
 
                _RootPath = HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath).ToLower();//當前的絕對路徑             
                if (_RootPath.Length == 1)  
                {  
                    _RootPath = "";  
                }  
                return _RootPath;  
            }  
        }

將Web網站下的絕對路徑轉換為相對於指定頁面的虛擬路徑

第一個方法
/**//// <summary>
///

將Web網站下的絕對路徑轉換為相對於指定頁面的虛擬路徑
/// </summary>
/// <param name="page">當前頁面指標,一般為this</param>
/// <param name="specifiedPath">絕對路徑</param>
/// <returns>虛擬路徑, 型如: http://www.cnblogs.com/</returns>
public static string ConvertSpecifiedPathToRelativePathForPage(Page page, string specifiedPath)
{
    // 根目錄虛擬路徑
    string virtualPath = page.Request.ApplicationPath;
    // 根目錄絕對路徑
    string pathRooted = HostingEnvironment.MapPath(virtualPath);
    // 頁面虛擬路徑
    string pageVirtualPath = page.Request.Path;    if (!Path.IsPathRooted(specifiedPath) || specifiedPath.IndexOf(pathRooted) == -1)
    {
        throw new Exception(string.Format("\"{0}\"是虛擬路徑而不是絕對路徑!", specifiedPath));
    }    // 轉換成相對路徑
    //(測試發現,pathRooted 在 VS2005 內建的伺服器跟在IIS下根目錄或者虛擬目錄運行似乎不一樣,
    // 有此地方後面會加"\", 有些則不會, 為保險起見判斷一下)
    if (pathRooted.Substring(pathRooted.Length - 1, 1) == "\\")
    {
        specifiedPath = specifiedPath.Replace(pathRooted, "/");
    }
    else
    {
        specifiedPath = specifiedPath.Replace(pathRooted, "");
    }    string relativePath = specifiedPath.Replace("\\", "/");    string[] pageNodes = pageVirtualPath.Split('/');    // 減去最後一個頁面和前面一個 "" 值
    int pageNodesCount = pageNodes.Length - 2;    for (int i = 0; i < pageNodesCount; i  )
    {
        relativePath = "/.."   relativePath;
    }    if (pageNodesCount > 0)
    {
        // 如果存在 ".." , 則把最前面的 "/" 去掉
        relativePath = relativePath.Substring(1, relativePath.Length - 1);
    }    return relativePath;
}  第二個方法將Web網站下的絕對路徑轉換為虛擬路徑
/**//// <summary>
/// 將Web網站下的絕對路徑轉換為虛擬路徑
/// 註:非Web網站下的則不轉換
/// </summary>
/// <param name="page">當前頁面指標,一般為this</param>
/// <param name="specifiedPath">絕對路徑</param>
/// <returns>虛擬路徑, 型如: ~/</returns>
public static string ConvertSpecifiedPathToRelativePath(Page page, string specifiedPath)
{
    string virtualPath = page.Request.ApplicationPath;    string pathRooted = HostingEnvironment.MapPath(virtualPath);    if (!Path.IsPathRooted(specifiedPath) || specifiedPath.IndexOf(pathRooted) == -1)
    {
        return specifiedPath;
    }    if (pathRooted.Substring(pathRooted.Length - 1, 1) == "\\")
    {
        specifiedPath = specifiedPath.Replace(pathRooted, "~/");
    }
    else
    {
        specifiedPath = specifiedPath.Replace(pathRooted, "~");
    }    string relativePath = specifiedPath.Replace("\\", "/");
    return relativePath;
}

將虛擬路徑轉絕對路就沒什麼好說的了, HttpRequest.MapPath 方法專門幹這事

 轉自http://www.notsee.info/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.