asp.net mvc 獲取物理路徑


https://blog.csdn.net/qq_42988813/article/details/88789783?depth_1-utm_source=distribute.pc_relevant.none-task-blog-OPENSEARCH-1&utm_source=distribute.pc_relevant.none-task-blog-OPENSEARCH-1

-----------------------------.cs類文件中

當前項目的應用名稱: HttpContext.Current.Request.ApplicationPath

當前項目的物理根路徑:  Request.PhysicalApplicationPath

當前項目的物理路徑嘛:     

  strPath   =   this.Server.MapPath(Request.PhysicalApplicationPath);     

  你要說明什么“類文件”。任何PAGE、CONTROL代碼也是在類中的。  
   
  上面的this只針對Page對象,針對control你應該替換為this.Page。你也可以使用:  
   
  strPath   =   HttpContext.Current.Server.MapPath

(HttpContext.Current.Request.PhysicalApplicationPath);  
   
  或者,使用:  
   
  strPath   =   AppDomain.CurrentDomain.BaseDirectory;  

--------------------------------帶page的頁面.cs中

很經常使用到的一個功能,但在在網上卻一直沒有找到相關的解決方法,今天借着項目應用到的機會寫了兩

個將絕對路徑轉換為虛擬路徑封裝好的方法
  將Web站點下的絕對路徑轉換為相對於指定頁面的虛擬路徑
  /**//// <summary>
  /// 將Web站點下的絕對路徑轉換為相對於指定頁面的虛擬路徑
  /// </summary>
  /// <param name="page">當前頁面指針,一般為this</param>
  /// <param name="specifiedPath">絕對路徑</param>
  /// <returns>虛擬路徑, 型如: ../../</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;
  }
  第二個方法顯然是從第一個方法中的前部分抽取出來的,所以懶得去添加相關注釋 :P
  將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 方法專門干這事

 

——————————————————————————————————————————————————

虛擬路徑:web頁面上的路徑,是相對於應用程序而言的。

  /// 將物理路徑轉換成相對路徑  
        /// </summary>  
        /// <param name="imagesurl1"></param>  
        /// <returns></returns>  
        private string urlToVirtual(string imagesurl1)  
        {  
             //其實這里的tmpRootDir也等於tmpRootDir</span><span style="font-size:18px;">=Server.MapPath(</span><span style="font-size:18px;">"~/");              
            string tmpRootDir = Server.MapPath(System.Web.HttpContext.Current.Request.ApplicationPath.ToString());//獲取程序根目錄  
            string imagesurl2 = imagesurl1.Replace(tmpRootDir, ""); //轉換成相對路徑  
            imagesurl2 = imagesurl2.Replace(@"\", @"/");  
            return imagesurl2;  
        }  
        //相對路徑轉換成服務器本地物理路徑  
        private string urlTolocal(string imagesurl1)  
        {  
            string tmpRootDir = Server.MapPath(System.Web.HttpContext.Current.Request.ApplicationPath.ToString());//獲取程序根目錄  
            string imagesurl2 = tmpRootDir + imagesurl1.Replace(@"/", @"\"); //轉換成絕對路徑  
            return imagesurl2;  
        }
 
————————————————
版權聲明:本文為CSDN博主「大佬兒」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/qq_42988813/article/details/88789783


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM