绝对路径返回web相对路径

时间:2008-08-06 08:34:14

标签: c# asp.net .net path mappath

如果我设法使用Server.MapPath找到并验证文件的存在,我现在想要将用户直接发送到该文件,那么转换该绝对路径的最快方式是什么回到相对的网络路径?

6 个答案:

答案 0 :(得分:53)

也许这可能会奏效:

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

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

答案 1 :(得分:36)

拥有 Server.RelativePath(路径)

是不是很好

嗯,你只需要扩展它; - )

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

答案 2 :(得分: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.");
}

答案 3 :(得分:4)

我喜欢Canoas的想法。不幸的是我没有“HttpContext.Current.Request”(BundleConfig.cs)。

我改变了这样的方法:

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

答案 4 :(得分:2)

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

答案 5 :(得分:0)

For asp.net core i wrote helper class to get pathes in both directions.

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

from Controller or service inject FilePathHelper and use:

var physicalPath = _fp.GetPhysicalPath("/img/banners/abro.png");

and versa

var virtualPath = _fp.GetVirtualPath(physicalPath);