C# 返回web相对路径的绝对路径

C# 返回web相对路径的绝对路径,c#,asp.net,.net,path,mappath,C#,Asp.net,.net,Path,Mappath,如果我已经使用Server.MapPath找到并验证了一个文件的存在性,现在我想直接将用户发送到该文件,那么将绝对路径转换回相对web路径的最快方法是什么?如果您使用Server.MapPath,那么您应该已经拥有相对web路径。根据,此方法采用一个变量path,即Web服务器的虚拟路径。因此,如果能够调用该方法,则应该已经可以立即访问相对web路径 也许这是可行的: String RelativePath = AbsolutePath.Replace(Request.ServerVariabl

如果我已经使用Server.MapPath找到并验证了一个文件的存在性,现在我想直接将用户发送到该文件,那么将绝对路径转换回相对web路径的最快方法是什么?

如果您使用Server.MapPath,那么您应该已经拥有相对web路径。根据,此方法采用一个变量path,即Web服务器的虚拟路径。因此,如果能够调用该方法,则应该已经可以立即访问相对web路径

也许这是可行的:

String RelativePath = AbsolutePath.Replace(Request.ServerVariables["APPL_PHYSICAL_PATH"], String.Empty);

我正在使用c#,但可以适应vb。

拥有服务器。相对路径(path)不是很好吗

那么,您只需要扩展它;-)

有了这个,你只需打电话就可以了

Server.RelativePath(path, Request);

我知道这是旧的,但我需要考虑虚拟目录(根据@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.");
}

我喜欢卡诺阿斯的想法。不幸的是,我没有可用的“HttpContext.Current.Request”(BundleConfig.cs)

我改变了方法如下:

public static string RelativePath(this HttpServerUtility srv, string path)
{
     return path.Replace(HttpContext.Current.Server.MapPath("~/"), "~/").Replace(@"\", "/");
}

对于asp.NETCore,我编写了helper类来获取双向路径

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

这不一定是真的-MapPath调用可能由另一个方法执行,并从我的文件检查器方法传入/调用,或者(在我的情况下)由许多不同的元素构建,其中一个元素是我的资源目录(定义为“~/\u资源”)。显然,将用户引导到此路径将导致意外结果。了解这一点也很有用,因为有时绝对文件路径可能会在没有其他上下文信息的情况下从数据库中拉出。-我不知道。按照定义方法的方式,如果输入到Server.MapPath的字符串有效并返回物理服务器路径,则该字符串也必须是有效的虚拟路径,无论您如何生成它。关于在虚拟地址开头使用波浪号(~)的问题,请参阅以下MSDN文章:>ASP.NET包含Web应用程序>根操作符(~),在服务器>cYaakov中指定路径时,可以使用>根操作符(~)。例如,我使用的函数采用根路径并递归返回
IEnumerable
集合。在我的web应用程序中,我可以提供此路径,将我的相对路径解析为物理路径,但当我返回此递归列表并希望将它们映射回我的应用程序中的相对路径时,我没有该信息。@:请注意,如果在网站中使用IIS虚拟目录,您的解决方案可能会失败,因为应用程序物理路径可能与文件的物理路径不同。更好的物理路径替代方法是~/。替换(context.ServerVariables(“APPL\u PHYSICAL\u path”),“~/”)
var virtualPath = _fp.GetVirtualPath(physicalPath);