Getting relative virtual path from physical path

前端 未结 4 706
青春惊慌失措
青春惊慌失措 2020-11-29 04:50

How can I get the relative virtual path from the physical path in asp.net? The reverse method is like below:

Server.MapPath(\"Virtual Path Here\");


        
相关标签:
4条回答
  • 2020-11-29 05:25

    Maybe this question is what you're looking for. There they suggest:

    String RelativePath = AbsolutePath.Replace(Request.ServerVariables["APPL_PHYSICAL_PATH"], String.Empty);
    
    0 讨论(0)
  • 2020-11-29 05:31
        public static string MapPathReverse(string fullServerPath)
        {            
            return @"~\" + fullServerPath.Replace(HttpContext.Current.Request.PhysicalApplicationPath,String.Empty);
        }
    
    0 讨论(0)
  • 2020-11-29 05:37

    You could also do something like this:

    string relativePath = absolutePath.Replace(HttpContext.Current.Server.MapPath("~/"), "~/").Replace(@"\", "/");
    

    The advantage is, that you don't need HttpContext.Current.Request.

    0 讨论(0)
  • 2020-11-29 05:41
    Request.ServerVariables["APPL_PHYSICAL_PATH"]
    

    is fine, but not always. It is available only if there's a HTTP request.

    On the other hand the call

    HostingEnvironment.ApplicationPhysicalPath
    

    is always available.

    0 讨论(0)
提交回复
热议问题