码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2017-12-19 00:55:33      阅读:280      评论:0      收藏:0      [点我收藏+]

标签:content   pos   连接   margin   cal   pad   color   不同   网站   

本文实例讲述了C#实现根据给出的相对地址获取网站绝对地址的方法。分享给大家供大家参考。具体分析如下:

这段C#代码在ASP.NET的项目中可以根据给定的相对地址获取绝对访问地址,例如:给出 /codes/index.php 可以返回http://www.jb51.net/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#程序设计有所帮助。

除声明外,跑步客文章均为原创,转载请以链接形式标明本文地址
  C#实现根据给出的相对地址获取网站绝对地址的方法

本文地址:  http://www.paobuke.com/develop/c-develop/pbk23060.html






相关内容

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

标签:content   pos   连接   margin   cal   pad   color   不同   网站   

原文地址:http://www.cnblogs.com/paobuke/p/8060002.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!