源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

C#实现根据给出的相对地址获取网站绝对地址的方法

  • 时间:2022-12-01 13:28 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:C#实现根据给出的相对地址获取网站绝对地址的方法
本文实例讲述了C#实现根据给出的相对地址获取网站绝对地址的方法。分享给大家供大家参考。具体分析如下: 这段C#代码在ASP.NET的项目中可以根据给定的相对地址获取绝对访问地址,例如:给出 /codes/index.php 可以返回http://www.1sucai.cn/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#程序设计有所帮助。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部