日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

ASP.NET根路径的获取和将Web站点下的绝对路径转换为虚拟路径的两种方案

發布時間:2024/10/8 asp.net 94 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ASP.NET根路径的获取和将Web站点下的绝对路径转换为虚拟路径的两种方案 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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/

轉載于:https://www.cnblogs.com/JuneZhang/archive/2010/11/23/1885671.html

總結

以上是生活随笔為你收集整理的ASP.NET根路径的获取和将Web站点下的绝对路径转换为虚拟路径的两种方案的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。