zoukankan      html  css  js  c++  java
  • asp.net mvc 获取物理路径

    https://blog.csdn.net/qq_42988813/article/details/88789783?depth_1-utm_source=distribute.pc_relevant.none-task-blog-OPENSEARCH-1&utm_source=distribute.pc_relevant.none-task-blog-OPENSEARCH-1

    -----------------------------.cs类文件中

    当前项目的应用名称: HttpContext.Current.Request.ApplicationPath

    当前项目的物理根路径:  Request.PhysicalApplicationPath

    当前项目的物理路径嘛:     

      strPath   =   this.Server.MapPath(Request.PhysicalApplicationPath);     

      你要说明什么“类文件”。任何PAGE、CONTROL代码也是在类中的。  
       
      上面的this只针对Page对象,针对control你应该替换为this.Page。你也可以使用:  
       
      strPath   =   HttpContext.Current.Server.MapPath

    (HttpContext.Current.Request.PhysicalApplicationPath);  
       
      或者,使用:  
       
      strPath   =   AppDomain.CurrentDomain.BaseDirectory;  

    --------------------------------带page的页面.cs中

    很经常使用到的一个功能,但在在网上却一直没有找到相关的解决方法,今天借着项目应用到的机会写了两

    个将绝对路径转换为虚拟路径封装好的方法
      将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, "");
       }
       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 方法专门干这事

    ——————————————————————————————————————————————————

    虚拟路径:web页面上的路径,是相对于应用程序而言的。

      /// 将物理路径转换成相对路径  
            /// </summary>  
            /// <param name="imagesurl1"></param>  
            /// <returns></returns>  
            private string urlToVirtual(string imagesurl1)  
            {  
                 //其实这里的tmpRootDir也等于tmpRootDir</span><span style="font-size:18px;">=Server.MapPath(</span><span style="font-size:18px;">"~/");              
                string tmpRootDir = Server.MapPath(System.Web.HttpContext.Current.Request.ApplicationPath.ToString());//获取程序根目录  
                string imagesurl2 = imagesurl1.Replace(tmpRootDir, ""); //转换成相对路径  
                imagesurl2 = imagesurl2.Replace(@"", @"/");  
                return imagesurl2;  
            }  
            //相对路径转换成服务器本地物理路径  
            private string urlTolocal(string imagesurl1)  
            {  
                string tmpRootDir = Server.MapPath(System.Web.HttpContext.Current.Request.ApplicationPath.ToString());//获取程序根目录  
                string imagesurl2 = tmpRootDir + imagesurl1.Replace(@"/", @""); //转换成绝对路径  
                return imagesurl2;  
            }
     
    ————————————————
    版权声明:本文为CSDN博主「大佬儿」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/qq_42988813/article/details/88789783

  • 相关阅读:
    工作中搜索页面搜索记录功能的封装(存储到本地)
    工作中遇到的git问题
    Git 常用命令
    帮女票做的报表(用到了angular的一些指令)
    HTML和CSS的知识点
    动画制作 手机APP制作以及响应式的实现
    CSS新内容
    JS中的循环嵌套 BOM函数
    fullpage的使用以及less, Sass的属性和JQuery自定义插件的声明和使用
    Javascript的内容
  • 原文地址:https://www.cnblogs.com/kelelipeng/p/12786949.html
Copyright © 2011-2022 走看看