Vra

As ek daarin geslaag het om die bestaan ​​van 'n lêer op te spoor en te verifieer met behulp van Server.MapPath en ek wil nou die gebruiker direk na daardie lêer stuur, wat is die vinnigste manier om daardie absolute pad terug te omskep in 'n relatiewe webpad?

Was dit nuttig?

Oplossing

Miskien kan dit werk:

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

Ek gebruik c# maar kan aangepas word na vb.

Ander wenke

Sal dit nie lekker wees om te hê nie Server.RelativePath(pad)?

wel, jy moet dit net verleng ;-)

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

Hiermee kan jy eenvoudig bel

Server.RelativePath(path, Request);

Ek weet dit is oud, maar ek moes rekenskap gee van virtuele gidse (per @Costo se opmerking).Dit blyk te help:

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

Ek hou van die idee van Canoas.Ongelukkig het ek nie "HttpContext.Current.Request" beskikbaar gehad nie (BundleConfig.cs).

Ek het die metode soos volg verander:

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

As jy Server.MapPath gebruik het, behoort jy reeds die relatiewe webpad te hê.Volgens die MSDN dokumentasie, hierdie metode neem een ​​veranderlike, pad, wat die virtuele pad van die webbediener is.So as jy die metode kon noem, behoort jy reeds die relatiewe webpad onmiddellik toeganklik te hê.

Vir asp.net kern het ek helper klas geskryf om paaie in beide rigtings te kry.

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

van kontroleerder of diens inspuit FilePathHelper en gebruik:

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

en omgekeerd

var virtualPath = _fp.GetVirtualPath(physicalPath);
Gelisensieer onder: CC-BY-SA met toeskrywing
Nie verbonde aan StackOverflow
scroll top