如果我已经设法使用 Server.MapPath 找到并验证文件的存在并且我现在想将用户直接发送到该文件,那么将该绝对路径转换回相对 Web 路径的最快方法是什么?
			
			58410 次
		
6 回答
            56        
        
		
也许这可能有效:
String RelativePath = AbsolutePath.Replace(Request.ServerVariables["APPL_PHYSICAL_PATH"], String.Empty);
我正在使用 c# 但可以适应 vb。
于 2008-08-06T09:28:50.940   回答
    
    
            38        
        
		
拥有Server.RelativePath(path)不是很好吗?
好吧,你只需要扩展它;-)
public static class ExtensionMethods
{
    public static string RelativePath(this HttpServerUtility srv, string path, HttpRequest context)
    {
        return path.Replace(context.ServerVariables["APPL_PHYSICAL_PATH"], "~/").Replace(@"\", "/");
    }
}
有了这个,你可以简单地打电话
Server.RelativePath(path, Request);
于 2011-03-07T17:27:02.383   回答
    
    
            13        
        
		
我知道这很旧,但我需要考虑虚拟目录(根据@Costo 的评论)。这似乎有帮助:
static string RelativeFromAbsolutePath(string path)
{
    if(HttpContext.Current != null)
    {
        var request = HttpContext.Current.Request;
        var applicationPath = request.PhysicalApplicationPath;
        var virtualDir = request.ApplicationPath;
        virtualDir = virtualDir == "/" ? virtualDir : (virtualDir + "/");
        return path.Replace(applicationPath, virtualDir).Replace(@"\", "/");
    }
    throw new InvalidOperationException("We can only map an absolute back to a relative path if an HttpContext is available.");
}
于 2012-05-05T14:05:48.737   回答
    
    
            5        
        
		
我喜欢 Canoas 的想法。不幸的是,我没有可用的“HttpContext.Current.Request”(BundleConfig.cs)。
我改变了这样的方法:
public static string RelativePath(this HttpServerUtility srv, string path)
{
     return path.Replace(HttpContext.Current.Server.MapPath("~/"), "~/").Replace(@"\", "/");
}
于 2015-06-24T08:19:45.037   回答
    
    
            2        
        
		
如果您使用了 Server.MapPath,那么您应该已经有了相对 Web 路径。根据MSDN 文档,此方法采用一个变量path,它是 Web 服务器的虚拟路径。因此,如果您能够调用该方法,您应该已经可以立即访问相对 Web 路径。
于 2008-08-06T08:44:34.820   回答
    
    
            0        
        
		
对于 asp.net 核心,我编写了辅助类来获取两个方向的路径。
public class FilePathHelper
{
    private readonly IHostingEnvironment _env;
    public FilePathHelper(IHostingEnvironment env)
    {
        _env = env;
    }
    public string GetVirtualPath(string physicalPath)
    {
        if (physicalPath == null) throw new ArgumentException("physicalPath is null");
        if (!File.Exists(physicalPath)) throw new FileNotFoundException(physicalPath + " doesn't exists");
        var lastWord = _env.WebRootPath.Split("\\").Last();
        int relativePathIndex = physicalPath.IndexOf(lastWord) + lastWord.Length;
        var relativePath = physicalPath.Substring(relativePathIndex);
        return $"/{ relativePath.TrimStart('\\').Replace('\\', '/')}";
    }
    public string GetPhysicalPath(string relativepath)
    {
        if (relativepath == null) throw new ArgumentException("relativepath is null");
        var fileInfo = _env.WebRootFileProvider.GetFileInfo(relativepath);
        if (fileInfo.Exists) return fileInfo.PhysicalPath;
        else throw new FileNotFoundException("file doesn't exists");
    }
从控制器或服务注入 FilePathHelper 并使用:
var physicalPath = _fp.GetPhysicalPath("/img/banners/abro.png");
反之亦然
var virtualPath = _fp.GetVirtualPath(physicalPath);
于 2018-12-19T11:40:03.870   回答