C#實現(xiàn)根據(jù)給出的相對地址獲取網站絕對地址的方法
更新時間:2015年03月26日 15:57:06 作者:feige
這篇文章主要介紹了C#實現(xiàn)根據(jù)給出的相對地址獲取網站絕對地址的方法,涉及C#URL及字符串操作的技巧,具有一定參考借鑒價值,需要的朋友可以參考下
本文實例講述了C#實現(xiàn)根據(jù)給出的相對地址獲取網站絕對地址的方法。分享給大家供大家參考。具體分析如下:
這段C#代碼在ASP.NET的項目中可以根據(jù)給定的相對地址獲取絕對訪問地址,例如:給出 /codes/index.php 可以返回http://www.dbjr.com.cn/codes/index.php的絕對地址結果。
/// <summary>
/// 根據(jù)給出的相對地址獲取網站絕對地址
/// </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#程序設計有所幫助。
相關文章
C#將數(shù)字轉換成字節(jié)數(shù)組的方法
這篇文章主要介紹了C#將數(shù)字轉換成字節(jié)數(shù)組的方法,涉及C#字符串操作的技巧,非常具有實用價值,需要的朋友可以參考下2015-04-04
在Framework 4.0中:找出新增的方法與新增的類(一)
經常看到有同學在討論Framework 4 的新特性,新方法,于是想寫個程序找出framework4.0中新增的方法和類2013-05-05

