If I have managed to locate and verify the existence of a file using Server.MapPath and I now want to send the user directly to that file, what is the fastest way to convert that absolute path back into a relative web path?
original title: "c# - Absolute path back to web-relative path"
If I have managed to locate and verify the existence of a file using Server.MapPath and I now want to send the user directly to that file, what is the fastest way to convert that absolute path back into a relative web path?
Server.MapPathを使用してファイルの存在を特定し、その存在を確認できた後、ユーザーをそのファイルに直接送信したい場合、その絶対パスを変換する最も速い方法は何ですか...
これは翻訳後の要約です。完全な翻訳を表示する必要がある場合は、「翻訳」アイコンをクリックしてください。
Perhaps this might work:
I'm using c# but could be adapted to vb.
Wouldn't it be nice to have Server.RelativePath(path)?
well, you just need to extend it ;-)
With this you can simply call
I know this is old but I needed to account for virtual directories (per @Costo's comment). This seems to help:
I like the idea from Canoas. Unfortunately I had not "HttpContext.Current.Request" available (BundleConfig.cs).
I changed the methode like this:
If you used Server.MapPath, then you should already have the relative web path. According to the MSDN documentation, this method takes one variable, path, which is the virtual path of the Web server. So if you were able to call the method, you should already have the relative web path immediately accessible.
For asp.net core i wrote helper class to get pathes in both directions.
from Controller or service inject FilePathHelper and use:
and versa