将Web站点下的绝对路径转换为虚拟路径

龙小宝小龙

龙小宝小龙

2016-02-19 18:59

今天图老师小编给大家介绍下将Web站点下的绝对路径转换为虚拟路径,平时喜欢将Web站点下的绝对路径转换为虚拟路径的朋友赶紧收藏起来吧!记得点赞哦~

  很经常使用到的一个功能,但在在网上却一直没有找到相关的解决方法,今天借着项目应用到的机会写了两个将绝对路径转换为虚拟路径封装好的方法
  将Web站点下的绝对路径转换为相对于指定页面的虚拟路径
  /**//// summary
  /// 将Web站点下的绝对路径转换为相对于指定页面的虚拟路径
  /// /summary
  /// param name="page"当前页面指针,一般为this/param
  /// param name="specifiedPath"绝对路径/param
  /// returns虚拟路径, 型如: ../..//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, "");
      }

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/webkaifa/)

      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;
  }

  第二个方法显然是从第一个方法中的前部分抽取出来的,所以懒得去添加相关注释 :P
  将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 方法专门干这事

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/webkaifa/)
展开更多 50%)
分享

猜你喜欢

将Web站点下的绝对路径转换为虚拟路径

Web开发
将Web站点下的绝对路径转换为虚拟路径

java(Web)中相对路径绝对路径问题

编程语言 网络编程
java(Web)中相对路径绝对路径问题

s8lol主宰符文怎么配

英雄联盟 网络游戏
s8lol主宰符文怎么配

如何把html中的相对路径变成绝对路径

Web开发
如何把html中的相对路径变成绝对路径

如何获取IIS虚拟目录的绝对路径 - PHPchina

PHP
如何获取IIS虚拟目录的绝对路径 - PHPchina

lol偷钱流符文搭配推荐

英雄联盟 网络游戏
lol偷钱流符文搭配推荐

HTML相对路径(Relative Path)和绝对路径(Absolute Path)

Web开发
HTML相对路径(Relative Path)和绝对路径(Absolute Path)

win7怎样获得文件夹绝对路径

电脑网络
win7怎样获得文件夹绝对路径

lolAD刺客新符文搭配推荐

英雄联盟
lolAD刺客新符文搭配推荐

把WebForm数据导出到Excel中

把WebForm数据导出到Excel中

Word2007打印省钱技巧

Word2007打印省钱技巧
下拉加载更多内容 ↓