本文實例講述了C#實現根據給出的相對地址獲取網站絕對地址的方法。分享給大家供大家參考。具體分析如下:
這段C#代碼在ASP.NET的項目中可以根據給定的相對地址獲取絕對訪問地址,例如:給出 /codes/index.php 可以返回//www.49028c.com/codes/index.php的絕對地址結果。
/// <summary>/// 根據給出的相對地址獲取網站絕對地址/// </summary>/// <param name="localPath">相對地址</param>/// <returns>絕對地址</returns>public static string GetWebPath(string localPath){ string path = HttpContext.Current.Request.ApplicationPath; string thisPath; string thisLocalPath; //如果不是根目錄就加上"/" 根目錄自己會加"/" if (path != "/") { thisPath = path + "/"; } else { thisPath = path; } if (localPath.StartsWith("~/")) { thisLocalPath = localPath.Substring(2); } else { return localPath; } return thisPath + thisLocalPath;}
希望本文所述對大家的C#程序設計有所幫助。
新聞熱點
疑難解答