Frage

Wenn ich es geschafft habe, zu suchen und zu überprüfen, ob eine Datei mit dem Server -.MapPath und ich will nun zu senden, die den Benutzer direkt auf die Datei, was ist das Schnellste Weg, um zu konvertieren, die absoluten Pfad in einen relativen webpfad?

War es hilfreich?

Lösung

Vielleicht funktionieren könnte:

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

Ich bin mit c#, aber könnte angepasst werden vb.

Andere Tipps

Wäre es nicht schön zu haben Server.RelativePath(Pfad)?

gut, Sie nur Notwendigkeit, es zu verlängern ;-)

public static class ExtensionMethods
{
    public static string RelativePath(this HttpServerUtility srv, string path, HttpRequest context)
    {
        return path.Replace(context.ServerVariables["APPL_PHYSICAL_PATH"], "~/").Replace(@"\", "/");
    }
}

Mit diesem können Sie einfach anrufen

Server.RelativePath(path, Request);

Ich weiß, das ist alt, aber ich musste Konto für virtuelle Verzeichnisse (pro @Costo Kommentar).Dies scheint zu helfen:

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

Ich mag die Idee von Canoas.Leider hatte ich nicht "HttpContext.Aktuelle.- Anfrage" zur Verfügung (BundleConfig.cs).

Ich habe die methode so:

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

Wenn Sie den Server.MapPath, dann sollten Sie bereits die relativen web-Weg.Nach der MSDN-Dokumentation, diese Methode nimmt eine variable, Pfad,, das ist der virtuelle Pfad des Web-Servers.Also, wenn Sie waren in der Lage zu rufen Sie die Methode, Sie sollten bereits die relativen webpfad sofort zugänglich.

Für asp.net core schrieb ich helper-Klasse, um Radwege in beide Richtungen.

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

vom Controller oder service injizieren FilePathHelper und verwenden:

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

und Umgekehrt

var virtualPath = _fp.GetVirtualPath(physicalPath);
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top