質問

Server.MapPath を使用してファイルの存在を見つけて確認できたので、ユーザーをそのファイルに直接送信したい場合、 最速の その絶対パスを相対Webパスに変換する方法はありますか?

役に立ちましたか?

解決

おそらくこれはうまくいくかもしれません:

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

私はC#を使用していますが、VBにも適応できる可能性があります。

他のヒント

あったらいいんじゃないでしょうか 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);

これが古いことはわかっていますが、仮想ディレクトリを考慮する必要がありました(@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(@"\", "/");
}

Server.MapPath を使用した場合は、相対 Web パスがすでに存在しているはずです。による MSDN ドキュメント, 、このメソッドは 1 つの変数を受け取ります。 パス, 、これは Web サーバーの仮想パスです。したがって、メソッドを呼び出すことができた場合は、相対 Web パスにすぐにアクセスできるようになっているはずです。

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);
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top